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

# WebSocket (SDK)

> Connect directly to the realtime stream from the SDK.

When you run the SDK in-process, connect to OnlyFans' realtime stream directly
with a short-lived auth token.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { connectRealtime } from "onlyfanskit";

const { token } = await of.users.authToken();

const ws = await connectRealtime({
  authToken: token,
  onEvent: (evt) => {
    if (evt.api === "newMessage") {
      // handle new message
    }
  },
});
```

The token is a JWT valid for roughly 24 hours. Refresh it by calling
`users.authToken()` again before it expires and reconnecting.

## Event handling

`onEvent` receives each decoded event. Branch on `evt.api` for the event type
(`newMessage`, and others). Keep the handler fast — offload heavy work to a
queue so you don't block the socket.

## When to prefer SSE instead

If you're calling the REST API rather than embedding the SDK, use
[Server-Sent Events](/realtime/server-sent-events) — it gives you the same
events without managing a WebSocket connection or token lifecycle.
