WebSocket Documentation¶
Overview¶
The BidScript backend uses WebSockets to provide real-time communication between the server and clients. WebSockets are used for features that require instant updates without polling, such as collaborative editing, chat notifications, and processing status updates.
Connection¶
Establishing a Connection¶
WebSocket connections are established at the following endpoint:
For secure connections in production:
Authentication¶
Authentication is required for WebSocket connections. Include the JWT token as a query parameter when connecting:
Namespaces¶
The WebSocket server is organised into different namespaces for specific functionality:
Main Namespace (/)¶
The default namespace for general events and notifications.
Events¶
Connection Events:
connect: Triggered when connection is establisheddisconnect: Triggered when connection is closederror: Triggered when an error occurs
Notification Events:
notification: General notifications
Status Update Events:
status:update: Processing status updates
Chat Namespace (/chat)¶
Dedicated to chat functionality.
Events¶
Message Events:
message:new: New message received
// Event payload
{
"sessionId": "string",
"messageId": "string",
"role": "string",
"content": "string",
"timestamp": "ISO date string"
}
message:update: Message content updated (for streaming responses)
Session Events:
session:update: Chat session updated
Editor Namespace (/editor)¶
For collaborative document editing.
Events¶
Document Events:
document:update: Document content updated
// Event payload
{
"documentId": "string",
"updates": [
{
"type": "string", // "insert", "delete", "format"
"index": number,
"length": number,
"attributes": object,
"userId": "string"
}
]
}
document:cursor: User cursor position update
Collaboration Events:
user:join: User joined the editing session
user:leave: User left the editing session
YJS Integration¶
The editor uses YJS for conflict-free collaborative editing:
import * as Y from "yjs";
import { WebsocketProvider } from "y-websocket";
// Create YJS document
const ydoc = new Y.Doc();
// Connect to WebSocket server with YJS provider
const provider = new WebsocketProvider(
"wss://api.bidscript.co.uk/editor/yjs",
"document-id",
ydoc,
{ params: { token: "your_jwt_token" } }
);
// Access shared types
const ytext = ydoc.getText("content");
// Listen for updates
ytext.observe((event) => {
console.log("Document updated:", event);
});
Client Implementation Example¶
import { io } from "socket.io-client";
// Connect to the WebSocket server
const socket = io("https://api.bidscript.co.uk/socket", {
query: { token: "your_jwt_token" },
transports: ["websocket"],
reconnection: true,
reconnectionAttempts: 5,
});
// Connect to specific namespace
const chatSocket = io("https://api.bidscript.co.uk/chat", {
query: { token: "your_jwt_token" },
});
// Listen for events
socket.on("connect", () => {
console.log("Connected to WebSocket server");
});
socket.on("status:update", (data) => {
console.log("Status update:", data);
});
chatSocket.on("message:new", (message) => {
console.log("New message:", message);
});
// Emit events
chatSocket.emit("join:session", { sessionId: "session-id" });
Error Handling¶
WebSocket errors are emitted through the error event:
Common error codes:
1000: Normal closure1001: Going away1002: Protocol error1003: Unsupported data1008: Policy violation1011: Internal server error
Reconnection Strategy¶
Socket.IO clients automatically handle reconnection. Configure reconnection settings: