Skip to main content

WebSocket

Prism has built-in WebSocket support with no external dependencies. It handles the HTTP upgrade handshake, frame parsing, and provides an actor-based connection model for safe concurrent messaging.

How It Works

  1. Client sends an HTTP request with Upgrade: websocket
  2. Prism validates the handshake and sends a 101 Switching Protocols response
  3. The connection switches to the WebSocket protocol
  4. Your handler receives connect, message, and disconnect events

Creating a Handler

Implement the PrismWebSocketHandler protocol:
Echo Handler

Registering WebSocket Routes

Register Handler

Building a Chat Server

Simple Chat Server

Client-Side Connection

Browser Client

Message Types

Real-Time Notifications Example

Notification System
For multi-room scenarios, check out WebSocket Rooms — it provides room management, broadcasting, and presence tracking out of the box.

Ping/Pong

Prism automatically responds to WebSocket ping frames with pong frames. This keeps connections alive through proxies and load balancers that might otherwise time out idle connections.