# Websock integration guide Websock sends JSON messages from your server to your users in real time. Your server publishes over HTTP. Browsers and apps receive over WebSocket. - API base URL: `https://dash.websock.dev` - Dashboard: `https://dash.websock.dev/admin` - This guide as plain text: `https://dash.websock.dev/docs.md` ## Concepts - **App**: one product or environment. It has its own channels, API keys, usage and daily message limit. Apps cannot see each other's channels. - **API key**: a server secret of the form `appID.keyID.secret`. It is shown once, when created. Keep it on your server. Never ship it to a browser or mobile app. - **Channel**: a named stream of messages, for example `news` or `user-42`. Channels are created on first use. Names use 1 to 128 characters from `A-Z a-z 0-9 _ - . = @ /`. - **Token grant**: what your server gives one signed-in user so that user can connect and subscribe. It lists exactly the channels that user may read. Clients can only receive. Only your server (with an API key) can publish. ## Flow 1. Your server asks Websock for a token grant for the signed-in user: `POST /v1/token`. 2. Your server returns the grant to that user's client. 3. The client connects to `ws_url` with `token`, and subscribes to each channel with its own subscription token. 4. Your server publishes: `POST /v1/publish`. Every subscribed client receives the message. Decide on your server which channels a user may read. Websock enforces the list you put in the grant. ## HTTP API Every request needs `Authorization: Bearer `. Bodies and responses are JSON. ### POST /v1/token Create a token grant for one user. ```json { "user_id": "42", "channels": ["news", "user-42"], "ttl_seconds": 3600, "info": { "name": "Ada" } } ``` - `user_id` (required): your ID for the user, up to 128 characters. - `channels`: up to 100 channels this user may subscribe to. - `ttl_seconds`: 60 to 86400. Default 3600. - `info`: any JSON. Other clients see it in presence and join/leave events. Response: ```json { "token": "", "expires_at": "2026-01-01T13:00:00Z", "ws_url": "wss://.../connection/websocket", "subscriptions": [ { "name": "news", "channel": "app:ab12cd34ef56.news", "token": "" } ] } ``` `name` is your channel name. `channel` is the full name the client must subscribe to. Always read `ws_url` and `channel` from the response; do not build them yourself. ### POST /v1/publish ```json { "channel": "news", "data": { "title": "Hello" } } ``` `data` is any JSON value except `null`. Response: `{ "ok": true }`. ### POST /v1/broadcast Send one message to several channels (1 to 100). Each channel counts as one message. ```json { "channels": ["user-1", "user-2"], "data": { "title": "Hello" } } ``` ### GET /v1/presence?channel=news Clients connected to a channel right now. ```json { "channel": "news", "clients": [ { "client": "", "user": "42", "conn_info": { "name": "Ada" } } ] } ``` ### GET /v1/history?channel=news&limit=50 Recent messages, newest first. `limit` is 1 to 100, default 50. ```json { "channel": "news", "publications": [ { "offset": 7, "data": { "title": "Hello" } } ] } ``` ### GET /v1/usage Messages published and token grants created per UTC day, last 30 days. ```json { "app_id": "ab12cd34ef56", "days": [ { "day": "2026-01-01", "messages": 120, "tokens": 14 } ] } ``` ### Errors Errors are `{ "error": "" }` with one of these statuses: | Status | Meaning | | --- | --- | | 400 | Invalid body, channel name or parameter. The message says which. | | 401 | API key is missing, wrong, revoked, or its app is disabled. | | 429 | The app's daily message limit is reached. It resets at 00:00 UTC. | | 502 | The messaging server refused the request. Retry later. | ### Example with curl ```sh curl -X POST https://dash.websock.dev/v1/publish \ -H "Authorization: Bearer $WEBSOCK_API_KEY" \ -H "Content-Type: application/json" \ -d '{"channel":"news","data":{"title":"Hello"}}' ``` ## Connect a client The WebSocket side speaks the Centrifugo protocol, so any Centrifugo client SDK works: JavaScript (`centrifuge`), Swift, Kotlin/Java, Dart/Flutter, Go, Python, C#. JavaScript, with `npm install centrifuge`: ```js import { Centrifuge } from 'centrifuge'; // Your own endpoint. It checks who the user is, then calls POST /v1/token. const getGrant = () => fetch('/api/realtime-token', { method: 'POST' }).then((r) => r.json()); const grant = await getGrant(); const client = new Centrifuge(grant.ws_url, { token: grant.token, getToken: async () => (await getGrant()).token, // called when the token expires }); const news = grant.subscriptions.find((s) => s.name === 'news'); const sub = client.newSubscription(news.channel, { token: news.token, getToken: async () => (await getGrant()).subscriptions.find((s) => s.name === 'news').token, }); sub.on('publication', (ctx) => console.log(ctx.data)); sub.on('join', (ctx) => console.log('joined', ctx.info.user)); sub.on('leave', (ctx) => console.log('left', ctx.info.user)); sub.subscribe(); client.connect(); ``` Notes: - The client reconnects by itself. After a short disconnect it receives the messages it missed. - In join, leave and client-side presence events the user is `.`. The HTTP presence endpoint returns your plain `user_id`. - Without a subscription token for a channel, the subscription is refused with "permission denied". ## Server example (Node, no dependencies) ```js const BASE = 'https://dash.websock.dev'; const call = async (method, path, body) => { const res = await fetch(BASE + path, { method, headers: { Authorization: 'Bearer ' + process.env.WEBSOCK_API_KEY, 'Content-Type': 'application/json' }, body: body && JSON.stringify(body), }); const json = await res.json(); if (!res.ok) throw new Error(`websock ${res.status}: ${json.error}`); return json; }; // In your token endpoint, after you have authenticated the user: const grant = await call('POST', '/v1/token', { user_id: user.id, channels: ['news', `user-${user.id}`] }); // Anywhere on your server: await call('POST', '/v1/publish', { channel: `user-${user.id}`, data: { type: 'order.shipped', id: 981 } }); ``` ## Limits | Limit | Value | | --- | --- | | Request body | 256 KB | | Channels per token grant or broadcast | 100 | | Token lifetime | 60 s to 24 h, default 1 h | | History per channel | last 100 messages, kept 5 minutes | | Daily messages | set per app in the dashboard; 0 means no limit | History is for catching up after a short disconnect. It is not long-term storage: keep your own copy of anything you need later. ## For AI agents: MCP server - URL: `https://dash.websock.dev/mcp` (HTTP transport) - Header: `Authorization: Bearer ` - The user creates the token in the dashboard under "MCP tokens". A read-only token can inspect; a full-access token can also create apps and keys and publish. Tools: `get_docs`, `list_apps`, `get_app`, `list_channels`, `get_presence`, `get_history`, `create_app`, `update_app`, `create_api_key`, `revoke_api_key`, `publish`. A typical integration by an agent: 1. `create_app` (or `list_apps` and `create_api_key` for an existing app). Put the returned `api_key` in the project's secret store as `WEBSOCK_API_KEY`. It cannot be read again. 2. Add a server endpoint that authenticates the user and calls `POST /v1/token`. 3. Add the client code from "Connect a client". 4. Publish from the server where events happen. 5. Check it: open the client, call `publish`, then `get_presence` and `get_history`.