Introduction
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
🎯 What does this module do?
The WS (WebSocket) module lets you receive real-time notifications - content updates, order changes, user actions - over a persistent connection, so the server pushes updates to you instantly instead of you polling for them. This enables live features like notifications, live chat, and real-time dashboards.
The single method, connect(), opens the connection and returns a socket.io Socket. It is synchronous and requires an authorized user, so authenticate first.
🚀 Quickstart
Initialize the module from defineOneEntry:
const { WS } = defineOneEntry( "your-project-url", { "token": "your-app-token" });
connect() is synchronous and returns a socket.io Socket (no await). Attach event handlers with socket.on(...):
// connect() requires an authorized user — authenticate first via AuthProvider.
const socket = WS.connect();
socket.on('connect', () => {
console.log('WebSocket connected');
});
// Listen for an event by its marker (configured in the Events module).
socket.on('my_event', (payload) => {
console.log('Update received!', payload);
});
✨ Key Concepts
What is WebSocket?
WebSocket is a persistent two-way connection between client and server:
- Persistent Connection - Stays open, no repeated handshakes
- Bidirectional - Both client and server can send messages
- Real-Time - Instant message delivery
- Event-Based - Listen to events with
socket.on(...) - Efficient - Low overhead compared to HTTP polling
WebSocket vs HTTP Polling
| Feature | WebSocket | HTTP Polling |
|---|---|---|
| Connection | Persistent (stays open) | Repeated connections |
| Latency | Sub-second | Depends on interval (2-30s) |
| Efficiency | Very efficient | Wasteful (many empty responses) |
| Server Load | Low (events only) | High (constant requests) |
| Use Case | Real-time updates | Acceptable for non-critical updates |
Connection Lifecycle
1. Authenticate the user (AuthProvider.auth)
↓
2. Open the connection: const socket = WS.connect()
↓
3. Wait for the 'connect' event: socket.on('connect', ...)
↓
4. Listen for events: socket.on('<event_marker>', callback)
↓
5. Server pushes events as they occur
↓
6. Disconnect when done: socket.disconnect()
What events arrive over the connection
There is no fixed catalog of SDK event names. Events fire according to your Events module configuration in the admin panel (the event must have the WebSocket option enabled). You listen for an event by its marker with socket.on('<event_marker>', callback).
The payload depends on the event's source. Typical payload fields (see connect() for full examples):
| Payload field | Arrives for | Contains |
|---|---|---|
attributes | Every event | The event's own attributes |
product | Catalog (product) events | Product attributes plus title |
user | User-form events | Fields from the user form |
order | Order events | Order id and attributes |
email / code | Registration / code forms | Additional form fields |
📋 What You Need to Know
Authorization is required
connect() requires an authorized user — call AuthProvider.auth(...) before connecting. The connection is authenticated with the user's access token plus your app token from defineOneEntry(). If no user is authorized, the SDK logs an error.
connect() is synchronous
connect() returns a socket.io Socket immediately — do not await it. Wait for the connect event before relying on the socket, then attach handlers with socket.on(...).
Subscribe after the connection opens, clean up when done
Attach event handlers after the connect event fires, and detach them (socket.off) plus call socket.disconnect() when the connection is no longer needed (e.g. on component unmount or user logout) to prevent memory leaks in SPAs.
Reconnection
socket.io reconnects automatically by default. To customize, listen for the disconnect and connect_error events and reconnect with exponential backoff, resetting the delay on a successful connect.
📊 Quick Reference Table
| Method | Description | Use Case |
|---|---|---|
| connect() 🔐 | Connect to WebSocket server | Establish connection |
🔐
connect()requires an authorized user — authenticate first via AuthProvider. After connecting, attach event handlers withsocket.on(...).
❓ Common Questions (FAQ)
How do I connect to the WebSocket server?
Use WS.connect() to open a connection — it is synchronous and returns a socket.io Socket. It requires an authorized user, so call AuthProvider.auth(...) first. Wait for the connect event before relying on the socket, then attach handlers with socket.on('<event_marker>', callback).
What events can I subscribe to?
There is no fixed SDK event list. Events are defined by your Events module configuration in the admin panel (with the WebSocket option enabled), and you listen for an event by its marker with socket.on('<event_marker>', callback). See connect() for payload examples (product, user, order, form).
How do I handle connection drops?
socket.io reconnects automatically by default. To customize, listen for the disconnect and connect_error events and reconnect with exponential backoff (e.g., 1s, 2s, 4s, 8s, up to a max), resetting the delay counter on a successful connect.
How do I prevent memory leaks with WebSocket subscriptions?
Detach event handlers (socket.off) and call socket.disconnect() when components unmount or the connection is no longer needed. In React, do this in useEffect cleanup; in Vue, in beforeUnmount.
🎓 Best Practices
- Authenticate first -
connect()requires an authorized user. - Don't await connect() - It's synchronous and returns a
Socket. - Subscribe after the connection opens - Attach handlers after the
connectevent fires. - Clean up - Detach handlers (
socket.off) andsocket.disconnect()to prevent memory leaks in SPAs. - Handle reconnection - socket.io auto-reconnects; customize with
disconnect/connect_errorand backoff if needed. - Validate event payloads - Payload shape depends on the event source; check fields before using them.
🔗 Related Documentation
- AuthProvider Module - Authorize the user before connecting
- Orders Module - Order events for real-time order tracking
- Users Module - User events for registration notifications
- WebSocket API