abstract class Azu::Channel

Overview

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.

Direct Known Subclasses

Defined in:

azu/channel.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(socket : HTTP::WebSocket) #

[View source]

Class Method Detail

def self.ws(path : Router::Path) #

Registers a WebSocket route


[View source]

Instance Method Detail

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

Handles the incoming WebSocket HTTP request


[View source]
abstract def on_binary(binary : Bytes) #

Invoked when a binary message is received


[View source]
abstract def on_close(code : HTTP::WebSocket::CloseCode | Nil, message : String | Nil) #

Invoked when the connection is closed


[View source]
abstract def on_connect #

Invoked when a connection is established


[View source]
abstract def on_message(message : String) #

Invoked when a text message is received


[View source]
abstract def on_ping(message : String) #

Invoked when a ping frame is received


[View source]
abstract def on_pong(message : String) #

Invoked when a pong frame is received


[View source]
def socket : HTTP::WebSocket #

[View source]
def socket? : HTTP::WebSocket | Nil #

[View source]