alepha@docs:~/docs/reference/primitives$
cat $websocket.md1 min read
#$websocket
#Import
typescript
1import { $websocket } from "alepha/websocket";
#Overview
Defines a WebSocket server endpoint for a specific channel.
Server-side only. Creates a WebSocket endpoint that:
- Accepts connections from clients
- Validates incoming messages against the channel schema
- Provides room-based messaging
- Integrates with alepha/security for authentication (optional)
- Supports horizontal scaling via alepha/topic
#Examples
typescript
1class ChatController { 2 chat = $websocket({ 3 channel: chatChannel, 4 handler: async ({ connectionId, userId, roomId, message, reply }) => { 5 // Broadcast to all in room except sender 6 await reply({ 7 message: { 8 type: "append", 9 username: userId,10 content: message.content11 },12 exceptSelf: true13 });14 }15 });16 17 async broadcastAnnouncement(roomId: string, text: string) {18 await this.chat.emit({19 roomId,20 message: {21 type: "append",22 username: "System",23 content: text24 }25 });26 }27}