Overview

WebSocket connections require an SDK token for authentication. The token is validated on connection and contains user and tenant context.
This page is for connecting to the WebSocket directly. If you’re using the SDK, it handles all of this for you. For how tokens are minted in the first place, see Authentication & tokens.

Connection with Token

Using Sec-WebSocket-Protocol (browsers)

Browsers cannot set custom headers on WebSocket. Pass the token as a subprotocol (from your backend):

Using Authorization Header (Node.js)

When using a WebSocket client that supports custom headers:

Using Query Parameter

Token Validation

The server validates:
  1. Token signature — Valid JWT signature
  2. Token expiration — Not expired (e.g. 24h lifetime)
  3. Token type — SDK token (e.g. sdk_user)
  4. App/tenant context — Valid app and tenant

Token Claims

Authentication Flow

App ID, public key, and secret key are backend-only. Your frontend receives only the token from your backend. Never expose keys to the frontend.

Getting an SDK Token (Backend)

Your backend issues tokens via POST /api/v1/apps/:appId/tokens. See Issue SDK Token. API key (server-to-server):
JWT: Use Authorization: Bearer <access_token> instead of API key headers. Return data.token to your frontend.

Error Handling

Invalid Token

Connection will be closed with code 1008 (Policy Violation).

Expired Token

Connection will be closed. Reconnect with a new token.

Token Refresh Strategy

Since SDK tokens expire after 15 minutes, implement token refresh:

Using the SDK

Use the token from your backend only—never keys:

Security Best Practices

Token Lifetime

SDK tokens are time-bound; refresh via your backend before expiry.

Use HTTPS/WSS

Always use secure connections.

Backend-only Keys

App ID, public key, and secret key stay on your backend. Frontend uses tokens only.

Don't Log Tokens

Never log tokens in production.

Next Steps

Message Types

WebSocket message types

Rooms

Room management

SDK Auth

SDK authentication

REST Auth

REST API authentication