class

Azu::Channel

Inherits HTTP::Handler < Reference < Object

A channel encapsulates a logical unit of work similar to an Endpoint.

Channels manage WebSocket connections, handling multiple instances where a single client may have multiple WebSocket connections open to the application.

Each channel can broadcast to multiple connected clients.

To set up a WebSocket route in your routing service:

ExampleApp.router do
  ws "/hi", ExampleApp::ExampleChannel
end

Pings and Pongs: The Heartbeat of WebSockets

After the handshake, either the client or the server can send a ping to the other party. Upon receiving a ping, the recipient must promptly send back a pong. This mechanism ensures that the client remains connected.

Constructors

new(socket : HTTP::WebSocket)
Source

Class methods

ws(path : Router::Path)

Registers a WebSocket route

Source

Instance methods

call(context : HTTP::Server::Context)

Handles the incoming WebSocket HTTP request

Source
on_binary(binary : Bytes)

Invoked when a binary message is received

Source
on_close(code : HTTP::WebSocket::CloseCode | Nil, message : String | Nil)

Invoked when the connection is closed

Source
on_connect

Invoked when a connection is established

Source
on_message(message : String)

Invoked when a text message is received

Source
on_ping(message : String)

Invoked when a ping frame is received

Source
on_pong(message : String)

Invoked when a pong frame is received

Source
socket
Source
socket?
Source