WebSocket
This document provides detailed documentation for the WebSocket controller based on the example implementation found in examples/controllers/socket.ts.
Overview
The Socket controller handles WebSocket connections and messages using decorators to define event handlers. It demonstrates how to manage client connections, subscribe to specific message types, and respond to WebSocket events.
Enable websockets
@Server({
controllers: [SocketController],
websocket: { path: "/ws" },
})
export class App {}
Decorators Explained
@Controller('socket'): Defines a WebSocket controller with the base route or namespacesocket.@OnConnection(): Marks a method to handle new WebSocket client connections.@Subscribe('eventName'): Marks a method to subscribe to a specific event type sent by clients.@OnMessage('eventName'): Marks a method to handle messages of a specific type.
Socket Controller Methods
onconnect(event: WebSocketEvent)
- Triggered when a new client connects.
- Sends a welcome message to the connected client.
sunbcription(event: WebSocketEvent)
- Subscribes to the
chatevent. - Checks the message content and ignores messages containing the word "bad".
onPing(event: WebSocketEvent)
- Handles
pingmessages. - Responds with a
pingmessage containing the current server time.
onChat(event: WebSocketEvent)
- Handles
chatmessages. - Responds with an
onChatmessage containing the current server time.
Example Usage
import { Controller } from "@heliosjs/core";
import {
OnConnection,
OnMessage,
Subscribe,
WebSocketEvent,
} from "@heliosjs/http";
@Controller("socket")
export class Socket {
@OnConnection()
onConnect(event: WebSocketEvent) {
return event.client.socket.send(
JSON.stringify({ type: "welcome", data: { message: "welcome" } }),
);
}
@Subscribe("chat")
subscribeToChat(event: WebSocketEvent) {
const msg = event.message?.data;
}
@OnMessage("ping")
onPing(event: WebSocketEvent) {
event.client.socket.send(
JSON.stringify({ type: "ping", data: { time: Date.now() } }),
);
}
@OnMessage("chat")
onChat(event: WebSocketEvent) {
event.client.socket.send(
JSON.stringify({ type: "onChat", data: { time: Date.now() } }),
);
}
}
Handling WebSocket Events
- When a client connects, the
onconnectmethod sends a welcome message. - Clients can send a
pingmessage to receive the current server time. - Clients can send a
chatmessage; messages containing "bad" are ignored by the subscription handler. - The
onChatmethod responds to chat messages with a timestamp.
Sending Messages Back to Clients
Messages are sent back to clients using the WebSocket send method on the client socket, with the message serialized as JSON. The message format includes a type field to identify the message type and a data field containing the payload.
Example:
event.client.socket.send(
JSON.stringify({ type: "ping", data: { time: Date.now() } }),
);
This structure allows clients to handle different message types appropriately.