Channel Generator

Generate WebSocket channels for real-time, bidirectional communication between clients and your Azu application.

Synopsis

azu generate channel <name> [actions...] [options]

Description

The channel generator creates WebSocket channel classes that enable real-time features like live updates, chat systems, notifications, and collaborative editing. Channels provide a clean abstraction for WebSocket connections with support for subscriptions, broadcasts, and custom action handlers.

Features

  • 🔌 WebSocket Communication: Full-duplex real-time connections

  • 📡 Broadcast Support: Send messages to all connected clients

  • 🎯 Action Handlers: Custom methods for different message types

  • 🔐 Authentication Ready: Integrate with your auth system

  • 📝 Automatic Logging: Built-in connection and message logging

  • 🔄 Auto-Reconnect: Client-side reconnection handling

  • 💬 Room/Stream Support: Group clients into channels or rooms

Usage

Basic Channel

Generate a basic channel with default actions:

This creates a ChatChannel with three default actions:

  • subscribed - Called when client connects

  • unsubscribed - Called when client disconnects

  • receive - Called when client sends data

Channel with Custom Actions

Generate a channel with specific custom actions:

Real-World Examples

Chat Channel

Presence Channel

Notifications Channel

Collaborative Editing

Arguments

Argument
Type
Description
Required

<name>

string

Channel name (PascalCase)

Yes

[actions...]

strings

Custom action method names

No

Options

Option
Description
Default

--force

Overwrite existing channel

false

Generated Files

Directory Structure

Channel File

The generator creates a channel class with the following structure:

Default Actions

subscribed

Called when a client connects to the channel:

unsubscribed

Called when a client disconnects:

receive

Called when receiving raw data from client:

Custom Actions

Define custom action methods to handle specific message types:

Client-Side JavaScript

The generator also provides reference JavaScript code for connecting to your channels:

Common Use Cases

1. Chat Application

2. Live Notifications

3. Presence Tracking

4. Collaborative Editing

Channel Methods

Broadcasting

Stream Management

Authentication

Server Setup

Mount Channels in Server

Configure Action Cable

Client Integration Examples

React/TypeScript

Vue.js

Testing

Example channel tests:

Best Practices

1. Authentication

Always authenticate users in subscribed:

2. Rate Limiting

Implement rate limiting for actions:

3. Error Handling

Handle errors gracefully:

4. Clean Up Resources

Always clean up in unsubscribed:

5. Use Specific Streams

Avoid broadcasting to all clients:

Troubleshooting

Connection Issues

Problem: WebSocket connection fails

Solutions:

  • Check server WebSocket endpoint is configured

  • Verify CORS settings

  • Check firewall/proxy settings

  • Ensure correct WebSocket URL (ws:// or wss://)

Messages Not Received

Problem: Broadcast messages not reaching clients

Solutions:

  • Verify client is subscribed to correct stream

  • Check stream name matches broadcast target

  • Ensure WebSocket connection is open

  • Check server logs for errors

Memory Leaks

Problem: Server memory grows over time

Solutions:

  • Properly clean up in unsubscribed

  • Don't store large objects in instance variables

  • Use weak references where appropriate

  • Monitor connection count

See Also

Last updated