Skip to main content
Allows you to subscribe to public topics.
interface WS {
  subscribe(
    topic: string | { [key: string]: any },
    callback: WSMessageHandler | Omit<WSMessageHandler, "onUnsubscribe">,
    once?: boolean,
    id?: string
  ): unsubscribe | undefined;
}
Subscribing to the trade topic:
const ws = useWS();

useEffect(() => {
  const unsubscribe = ws.subscribe(
    {
      id: `${symbol}@trade`,
      event: "subscribe",
      topic: `${symbol}@trade`,
      ts: Date.now()
    },
    {
      onMessage: (data: any) => {
        //
      }
    }
  );
  () => {
    // Unsubscribes when the component unloads
    unsubscribe();
  };
}, []);

Subscribing to private topics

Subscribing to the executionreport topic:
const ws = useWS();

useEffect(() => {
  const unsubscript = ws.privateSubscribe(
    {
      id: "executionreport",
      event: "subscribe",
      topic: "executionreport",
      ts: Date.now()
    },
    {
      onMessage: (data) => {
        // do something
      }
    }
  );
  () => {
    // Unsubscribes when the component unloads
    unsubscribe();
  };
}, []);