Using WebSocket with Load Balancer

Learn how to use WebSocket with Load Balancer.


Websocket is a communication protocol for building real-time applications. It allows you to establish a bidirectional communication channel between the client and the server, enabling you to send and receive messages in real-time. You can use your Load Balancing strategies with Websockets as well.

Using WebSocket with Load Balancer

To use WebSocket with Load Balancer, you need to pass in an additional request header x-ironforge-method with the method name. You can find the method names Solana RPC supports here: Solana RPC WebSocket Methods

Websocket methods can be configured with custom endpoints using the Dynamic Strategy in the Load Balancer's Routing Strategies tab. If no specific Dynamic Strategy is set for a method, the system defaults to the Primary Routing Strategy. For more details on routing, refer to the Routing section.

The following example demonstrates how to use WebSocket with Load Balancer:

import WebSocket from "ws";

const WEBSOCKET_URL = "wss://rpc.ironforge.network/mainnet?apiKey=YOUR_API_KEY";
const methodName = "programSubscribe"; // method you want to call

const ws = new WebSocket(WEBSOCKET_URL, {
  headers: {
    "x-ironforge-method": methodName,
  },
});

ws.on("open", () => {
  console.log("Connected to Ironforge WebSocket");

  // Call a method after connection is established
  const requestPayload = {
    jsonrpc: "2.0",
    method: methodName,
    params: [
      "11111111111111111111111111111111",
      {
        encoding: "base64",
        commitment: "finalized",
      },
    ], // Add your parameters here if needed
    id: 1,
  };

  ws.send(JSON.stringify(requestPayload));
});

ws.on("message", (data) => {
  console.log("Received:", data);
});

ws.on("error", (error) => {
  console.error("WebSocket error:", error);
});

ws.on("close", () => {
  console.log("WebSocket connection closed");
});