Skip to main content
The publish/subscribe pattern is at the heart of the live package. Sessions subscribe to topics, and when you publish to a topic, all subscribers receive the message.

How Pub/Sub Works

Subscribing

Subscribe sessions to topics via server methods:

Publishing

To All Subscribers

With JSON Data

To a Single Session

Topic Naming Patterns

Use consistent, hierarchical names:

User Topics

Room/Channel Topics

Event Topics

Resource Topics

Common Patterns

Chat Room

Notifications

Real-time Updates

Presence

Publishing from HTTP Handlers

You can publish from any part of your application:

Message Format

Messages sent to clients have this format:
Client-side parsing:

Performance Tips

Topic Granularity

Message Size

Fan-out

Be aware of high fan-out:

Subscription Validation

Always validate subscriptions:

Best Practices

1. Use Namespaced Topics

2. Document Conventions

3. Consistent Message Format