πŸ”ŒStandard Websockets

Stream data directly to your applications with our websocket integration.

Websockets allow for two-way communication between a client and a server. Unlike traditional request-response models, Websockets keep a persistent connection open, enabling real-time data exchange. This is perfect for applications that require instant updates, such as chat apps, online games and marketplaces.

Standard Websockets

Helius supports all stable Solana Websockets. You can find a list of all these Websockets in the Solana documentation.

You can use these with your Helius WSS URL:

  • Mainnet – wss://mainnet.helius-rpc.com

  • Devnet – wss://devnet.helius-rpc.com

WebSocket methods marked as "unstable" in Solana's documentation, such as blockSubscribe, slotsUpdatesSubscribe and voteSubscribe are not supported.

Javascript Examples

Program Susbcribe
const WebSocket = require('ws');

// Create a WebSocket connection
const ws = new WebSocket('wss://mainnet.helius-rpc.com/?api-key=<api-key>');

// Function to send a request to the WebSocket server
function sendRequest(ws) {
    const request = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "programSubscribe",
        "params": [
          "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P",
          {
            "encoding": "jsonParsed"
          }
        ]
      };
    ws.send(JSON.stringify(request));
}

// Function to send a ping to the WebSocket server
function startPing(ws) {
    setInterval(() => {
        if (ws.readyState === WebSocket.OPEN) {
            ws.ping();
            console.log('Ping sent');
        }
    }, 30000); // Ping every 30 seconds
}

// Define WebSocket event handlers

ws.on('open', function open() {
    console.log('WebSocket is open');
    sendRequest(ws); // Send a request once the WebSocket is open
    startPing(ws); // Start sending pings
});

ws.on('message', function incoming(data) {
    const messageStr = data.toString('utf8');
    try {
        const messageObj = JSON.parse(messageStr);
        console.log('Received:', messageObj);
    } catch (e) {
        console.error('Failed to parse JSON:', e);
    }
});

ws.on('error', function error(err) {
    console.error('WebSocket error:', err);
});

ws.on('close', function close() {
    console.log('WebSocket is closed');
});
Signature Subscribe

This subscription is for a single notification. The server automatically cancels it after sending the signatureNotification via the RPC.

const WebSocket = require('ws');

// Create a WebSocket connection
const ws = new WebSocket('wss://mainnet.helius-rpc.com/?api-key=<api-key>');

// Function to send a request to the WebSocket server
function sendRequest(ws) {
    const request = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "signatureSubscribe",
        "params": [
          "2EBVM6cB8vAAD93Ktr6Vd8p67XPbQzCJX47MpReuiCXJAtcjaxpvWpcg9Ege1Nr5Tk3a2GFrByT7WPBjdsTycY9b",
          {
            "commitment": "finalized",
            "enableReceivedNotification": false
          }
        ]
      };
    ws.send(JSON.stringify(request));
}

// Function to send a ping to the WebSocket server
function startPing(ws) {
    setInterval(() => {
        if (ws.readyState === WebSocket.OPEN) {
            ws.ping();
            console.log('Ping sent');
        }
    }, 30000); // Ping every 30 seconds
}

// Define WebSocket event handlers

ws.on('open', function open() {
    console.log('WebSocket is open');
    sendRequest(ws); // Send a request once the WebSocket is open
    startPing(ws); // Start sending pings
});

ws.on('message', function incoming(data) {
    const messageStr = data.toString('utf8');
    try {
        const messageObj = JSON.parse(messageStr);
        console.log('Received:', messageObj);
    } catch (e) {
        console.error('Failed to parse JSON:', e);
    }
});

ws.on('error', function error(err) {
    console.error('WebSocket error:', err);
});

ws.on('close', function close() {
    console.log('WebSocket is closed');
});

Last updated