> ## Documentation Index
> Fetch the complete documentation index at: https://orderly.network/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# useWS

> Hook to subscribe to public WebSocket topics with custom message handlers.

Allows you to subscribe to public topics.

* [Tech docs](/sdks/tech-doc/modules/orderly_network_hooks#usews)

```tsx theme={null}
interface WS {
  subscribe(
    topic: string | { [key: string]: any },
    callback: WSMessageHandler | Omit<WSMessageHandler, "onUnsubscribe">,
    once?: boolean,
    id?: string
  ): unsubscribe | undefined;
}
```

Subscribing to the `trade` topic:

```tsx theme={null}
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:

```tsx theme={null}
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();
  };
}, []);
```
