WS
Open a real-time WebSocket connection to receive content, order, and user updates without polling.
More information about the module's user interface https://doc.oneentry.cloud/docs/events/introduction (opens in a new tab)
The WS (WebSocket) module delivers real-time notifications (content updates, order changes, user actions) over a persistent connection, so you don't have to poll. Its single method, connect(), opens the connection and returns a socket.io Socket.
Quickstart
Initialize the module from defineOneEntry:
const { } = defineOneEntry( , { });
connect() is synchronous: it returns the Socket right away, so don't await it. Attach event handlers with socket.on(...):
// The SDK logs "User is unauthorized" if no user is signed in; see Authentication below.
const socket = WS.connect();
socket.on('connect', () => {
console.log('WebSocket connected');
});
// Every event arrives as 'notification'; use payload.type to tell sources apart.
socket.on('notification', (payload) => {
if (payload.type === 'order') {
console.log('Order update', payload.order);
}
});
Connection lifecycle
1. Authenticate the user (AuthProvider.auth)
↓
2. Open the connection: const socket = WS.connect()
↓
3. Register handlers right away: socket.on('connect', ...)
↓
4. Listen for events: socket.on('notification', callback)
↓
5. Server pushes events as they occur
↓
6. Disconnect when done: socket.disconnect()
Events
All events arrive on a single socket.io event, notification. The event marker is not used as the event name, so socket.on('<event_marker>', ...) never fires. Which events are sent depends on the Events module configuration in the admin panel, and only events with the WebSocket option enabled are sent.
The payload contains message (the event's message text; HTML for workflow notifications) plus fields that depend on the source. Every payload includes type: product, order, user, form or refund. Notifications sent from a workflow have type: "notification" and the event marker in eventIdentifier. Check the fields before using them (see connect() for full examples):
| Payload field | Arrives for | Contains |
|---|---|---|
message | Every event | The event's message text |
type | Every event | Source of the event, such as order |
attributes | Product, order, user, form and refund events | The event's own attributes |
product | Product events | id, status, info.title, attributes |
user | User, order and refund events | notificationData, attributes |
order | Order events | Order id and attributes |
refund | Refund events | id, amount, note, sessions |
email / formData | Form events (reply or status mode) | The user's email and the form data |
eventIdentifier | Workflow notifications | Marker of the event |
Authentication
The SDK does not currently send device metadata with the socket handshake, so the server registers every connection as a guest, in both browsers and Node.js. A guest socket receives only events sent to everyone; events addressed to registered users or to a specific user are not delivered. In browsers, the Authorization and x-app-token headers aren't sent either, because the browser WebSocket API doesn't support custom headers. Without a signed-in user, the SDK logs User is unauthorized and still returns a socket.
socket.io is loaded on demand
socket.io-client (~41 kB) is imported the first time connect() is called, so projects that never open a socket don't ship it. See Bundle Size & Module Formats.
Until the chunk loads, the returned object queues whatever you call on it (on, emit, disconnect) and replays it onto the real socket in the same tick the socket is created, before the connection can deliver anything, so no event is lost. Reading state early is accurate too: a freshly created socket isn't connected yet, so id is undefined and connected is false.
The one difference: a method that has to return a value (e.g. listeners()) can't answer before the chunk arrives, and nested objects such as socket.io are reachable only after it loads.
Cleaning up
Register handlers once, right after connect(), not inside a connect handler, which would add a duplicate on every reconnect. When the connection is no longer needed (for example on component unmount or logout), remove the handlers with socket.off and call socket.disconnect() to prevent memory leaks in SPAs. In React, do this in the useEffect cleanup; in Vue, in beforeUnmount.
Reconnection
socket.io reconnects automatically with exponential backoff (1 s up to 5 s between attempts). connect() doesn't accept socket.io options, so these defaults can't be changed. If the server closes the socket itself, socket.io doesn't reconnect; call WS.connect() again.
Methods
| Method | Description | Use Case |
|---|---|---|
| connect() | Open the WebSocket connection | Establish connection |
Related
- AuthProvider Module: sign the user in
- Orders Module: order events for real-time order tracking
- Users Module: user events for registration notifications
- socket.io client API