tezz
Tezz Documentation

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. The socket object 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 upgrade event using the battle-tested ws library.
  • Cloudflare Workers: Automatically intercepts Upgrade: websocket requests and establishes a native WebSocketPair() returning HTTP 101 Switching Protocols.