Declarative WebSockets
Tezz makes real-time bidirectional communication as easy as defining an HTTP route. Define full-featured WebSocket servers inside your service blocks with zero boilerplate.
The socket Block
Declare a WebSocket endpoint alongside standard HTTP routes using the socket declaration:
service ChatAPI on 3000 {
route GET "/health" {
respond 200 { status: "ok" }
}
socket "/ws" {
on connect {
print("Client connected!")
}
on message(data) {
print("Received: " + data)
-- Automatically serializes JSON objects or strings
socket.send({ echo: data })
}
on close {
print("Client left")
}
}
}
Lifecycle Events
on connect { ... }: Invoked when a new client completes the WebSocket upgrade handshake. Thesocketobject is available in scope.on message(data) { ... }: Triggered on incoming frames. Automatically parses incoming JSON payloads when applicable.on close { ... }: Invoked when the client closes the connection.on error(err) { ... }: Captures socket-level communication errors.
Node.js & Edge Workers
- Node.js: Tezz wires into the native HTTP server
upgradeevent using the battle-testedwslibrary. - Cloudflare Workers: Automatically intercepts
Upgrade: websocketrequests and establishes a nativeWebSocketPair()returning HTTP 101 Switching Protocols.