𝗪𝗲𝗯𝗦𝗼𝗰𝗸𝗲𝘁 𝗮𝗻𝗱 𝗦𝗦𝗘 𝗶𝗻 𝗛𝘆𝗽𝗲𝗿𝗹𝗮𝗻𝗲
Real-time data is vital for modern apps. You need it for chat, live dashboards, and notifications.
Hyperlane is a fast Rust HTTP server library. It supports two main ways to send real-time data: WebSocket and Server-Sent Events (SSE).
WebSocket vs SSE
WebSocket is bidirectional. Both the client and server can send messages at any time. It uses a single TCP connection. Use this for chat apps or online games.
SSE is unidirectional. Only the server sends data to the client. It uses standard HTTP. Use this for news feeds or stock tickers.
WebSocket in Hyperlane
Hyperlane uses attribute macros to make WebSockets easy.
- Use #[is_ws_upgrade_type] to detect a WebSocket request.
- Use #[try_get_websocket_request] to get the data.
You can manage the connection life cycle with simple commands. You can send individual frames or lists of frames. You can also use tools like hyperlane-broadcast to send messages to many users at once.
SSE in Hyperlane
SSE is simpler to set up. You only need to set one header: Content-Type: text/event-stream.
The browser handles reconnection automatically. This makes it very reliable for simple data streams. Each message follows a simple text format: data:
Quick Comparison
• Direction: WebSocket is two-way. SSE is server-to-client only. • Protocol: WebSocket uses ws://. SSE uses standard HTTP. • Reconnection: You must handle WebSocket reconnects manually. Browsers do it for SSE. • Data Type: WebSocket supports binary data. SSE supports text only. • Complexity: WebSocket is harder to build. SSE is easier.
Choose WebSocket if you need low latency and two-way talk. Choose SSE if you need a simple way to push updates to users.
Hyperlane gives you the tools to build both efficiently in Rust.
Project Code: https://github.com/hyperlane-dev/hyperlane