Skip to main content
This WebSocket API will be deprecated on July 1, 2026. See the Migration Guide to move to the Centrifuge-based API.
We use the Ably SDK to allow users to connect to our API. It supports pretty much every major language but all of the examples on this page will be in JavaScript. The API is relatively identical across languages though. See this link for a basic overview of the API in other languages.
You only need one instance of the ably object to connect to the API. Connections to multiple channels are multiplexed though the single network connection. If you create too many individual connections, you will be forcefully unsubscribed from all channels and disconnected.
All the examples following assume you have a realtime object in scope following the initialization code to the right.
import * as ably from "ably";

async function createTokenRequest() {
  const response = await fetch("https://api.sx.bet/user/token", {
    headers: {
      "X-Api-Key": process.env.SX_BET_API_KEY,
    },
  });
  return response.json();
}

async function initialize() {
  const realtime = new ably.Realtime.Promise({
    authCallback: async (tokenParams, callback) => {
      try {
        const tokenRequest = await createTokenRequest();
        // Make a network request to GET /user/token passing in
        // `X-Api-Key: [YOUR_API_KEY]` as a header
        callback(null, tokenRequest);
      } catch (error) {
        callback(error, null);
      }
    },
  });
  await ablyClient.connection.once("connected");
}