Skip to main content

Install

npm install centrifuge
Centrifugo provides official client SDKs for most platforms:
SDKLanguage / Platform
centrifuge-jsJavaScript — browser, Node.js, React Native
centrifuge-pythonPython (asyncio)
centrifuge-goGo
centrifuge-dartDart / Flutter
centrifuge-swiftSwift (iOS)
centrifuge-javaJava / Android
centrifuge-csharpC# (.NET, MAUI, Unity)
For the full list including community SDKs, see the Centrifugo client SDK docs.

Connect

Fetch a token using your API key, then instantiate and connect the Centrifuge client. You only need one client instance — all channel subscriptions are multiplexed over the single connection. If you need more than 512 subscriptions, create additional client instances (each connection supports up to 512 channels). See Limits for details.
import { Centrifuge } from "centrifuge";

const RELAYER_URL = "https://api.sx.bet"; // Mainnet — use https://api.toronto.sx.bet for testnet
const WS_URL = "wss://realtime.sx.bet/connection/websocket"; // Mainnet — use wss://realtime.toronto.sx.bet/connection/websocket for testnet

async function fetchCentrifugoToken(apiKey) {
  const res = await fetch(`${RELAYER_URL}/user/realtime-token/api-key`, {
    headers: { "x-api-key": apiKey },
  });
  if (!res.ok) {
    const body = await res.text();
    throw new Error(`Token endpoint returned ${res.status}: ${body}`);
  }
  const { token } = await res.json();
  return token;
}

const client = new Centrifuge(WS_URL, {
  getToken: () => fetchCentrifugoToken(YOUR_API_KEY),
});

client.connect();

Subscribe to a channel

Once connected, create a subscription for the channel you want. All channel pages in this section use this same pattern — replace "channel:name" with the channel name format documented on each page.
const sub = client.newSubscription("channel:name", { positioned: true, recoverable: true });

sub.on("publication", (ctx) => {
  const data = ctx.data;
  // handle incoming message
});

sub.subscribe();
Pass positioned: true, recoverable: true together on channels with history enabled to get at-least-once delivery across reconnects. See Namespace history capabilities for which channels support this, and Best Practices for how to handle recovery outcomes.
Each publication event exposes a ctx object with the following structure:
{
  "channel": "parlay_markets:global",
  "data": { },
  "tags": {
    "publishedAt": "1773869618503",
    "messageId": "5db68f78-8442-4530-8476-62495333e9ee"
  }
}
ctx.data is the channel payload documented on each channel’s reference page. ctx.tags.messageId is a UUID present on every publication — use it to deduplicate messages during history recovery (see Real-time Data → Dedup).

Cleanup

When you no longer need a subscription, clean it up to free resources:
sub.unsubscribe();
sub.removeAllListeners();
client.removeSubscription(sub);