alepha@docs:~/framework/docs/reference/react-hooks$
cat useRoom.md | pretty
1 min read
Last commit:

#useRoom

#Import

typescript
1import { useRoom } from "alepha/react/websocket";

#Overview

React hook for WebSocket room communication

Provides automatic connection management, reconnection, and type-safe messaging for WebSocket rooms using the injected WebSocketClient service.

Multiple useRoom hooks on the same channel will share a single WebSocket connection.

#Examples

tsx
 1const chat = useRoom( 2  { 3    roomId: "room-123", 4    channel: chatChannel, 5    handler: (message) => { 6      if (message.type === "append") { 7        setMessages((prev) => [...prev, message]); 8      } 9    },10  },11  [roomId],12);13 14const sendMessage = async () => {15  await chat.send({16    content: "Hello, world!",17  });18};