-
Notifications
You must be signed in to change notification settings - Fork 2
WebSockets
BrewedSys edited this page Mar 30, 2026
·
1 revision
Http-native supports WebSocket connections through a Rust-backed upgrade mechanism.
import { createApp } from "@http-native/core";
const app = createApp();
app.ws("/chat", (ws) => {
console.log("Client connected");
ws.on("message", (data) => {
console.log("Received:", data);
ws.send(`Echo: ${data}`);
});
ws.on("close", () => {
console.log("Client disconnected");
});
});
await app.listen({ port: 8190 });- The client sends an HTTP upgrade request to the WebSocket path
- The Rust layer handles the WebSocket handshake natively
- Once upgraded, frames are parsed and dispatched to your JavaScript handler
- Messages are sent back through the Rust layer for efficient socket writes
| Event | Callback | Description |
|---|---|---|
message |
(data) => void |
Received a message from the client |
close |
() => void |
Connection was closed |
error |
(err) => void |
An error occurred |
| Method | Description |
|---|---|
ws.send(data) |
Send a message to the client |
ws.close() |
Close the connection |
const clients = new Set();
app.ws("/chat", (ws) => {
clients.add(ws);
ws.on("message", (msg) => {
for (const client of clients) {
if (client !== ws) client.send(msg);
}
});
ws.on("close", () => {
clients.delete(ws);
});
});const ws = new WebSocket("ws://localhost:8190/chat");
ws.onopen = () => ws.send("Hello!");
ws.onmessage = (event) => console.log("Server says:", event.data);Http-native is open to everyone, but we have strict regulations and licensing fees for large organizations