---
title: WebSocket
project: OpenSync
version: 1.0
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/opensync/1-0/reference/websocket/
updated: 2026-11-19T09:22:09.965Z
---

# WebSocket

> Connect over WebSocket to receive messages, and send messages to a channel.

A WebSocket client both receives messages and can send them. All messages are JSON text.

## Connect

```text
wss://sync.example.com/?token=YOUR_TOKEN&app=Dashboard
```

| Query argument | Meaning |
|---|---|
| `token` | An [access token](../../guides/access-tokens/), or the setup token. |
| `app` | Optional. The name shown on the [Clients](../../guides/clients/) page. |

Use `ws://` when OpenSync runs without HTTPS, for example `ws://localhost:8000/?token=YOUR_TOKEN`.

Right after the connection opens, OpenSync sends an `init` message:

```json
{"type":"init","name":"Demo sync","version":1,"id":"OpenSync"}
```

`name` is the instance name from the **Setup** page. OpenSync sends `init` again to every connected client when someone clicks **APPLY** on the **Setup** page.

## Receive messages

Every request to a channel that the token can read arrives as one [message](../message-format/):

```json
{"id":"24nad002tl50d","type":"application/json","channel":"orders","method":"POST","headers":{"host":"sync.example.com","connection":"keep-alive","content-type":"application/json","accept":"*/*","accept-language":"*","sec-fetch-mode":"cors","user-agent":"node","accept-encoding":"gzip, deflate","content-length":"24"},"ua":"","query":{},"body":{"id":1002,"total":49.9},"ts":"2026-11-19T07:37:50.820Z","ip":"203.0.113.10","files":[]}
```

Tell `init` apart from the other messages by `"type":"init"`.

## Send a message

Send a `notify` message with the channel and your data in `body`:

```json
{"type":"notify","channel":"chat","body":{"text":"Hello"}}
```

Every client that can read `chat` receives it, the sender included:

```json
{"id":"24nad007tl51d","type":"application/json","channel":"chat","method":"SOCKET","headers":{},"ua":"","query":{},"body":{"text":"Hello"},"ts":"2026-11-19T07:37:52.847Z","ip":"203.0.113.10","files":[]}
```

`method` is `SOCKET`, and `headers` and `query` are empty. You can name the data field `data` instead of `body`. Messages without a `channel`, or that are not JSON, are ignored.

## When the connection closes

| Close code | Reason |
|---|---|
| `4001` | The token is wrong, was removed, or your IP address is locked after failed attempts. See [Access problems](../../faq/access/). |

OpenSync does not keep messages for clients that are offline. Reconnect after a close; see [Listen from Node.js](../../recipes/nodejs-client/).
