-
-
Notifications
You must be signed in to change notification settings - Fork 1
topic controller
Actionbot edited this page Mar 26, 2025
·
1 revision
TopicsController is a utility class for managing topic-based subscriptions in real-time applications. It handles subscribing to topics, listening for specific events, and managing subscription lifecycles.
const topicsController = new TopicsController(client);| Parameter | Type | Description |
|---|---|---|
| client | Object | RTEngine client |
| Property | Type | Description |
|---|---|---|
| subscribed | Set | Stores currently subscribed topics |
Registers a callback for a specific event on a given topic.
topicsController.on('chat/room1', 'message', (data, payload) => {
console.log('Message received:', data);
});| Parameter | Type | Description |
|---|---|---|
| topic | string | Topic to associate the event with |
| event | string | Name of the event to listen for |
| callback | Function | Function to execute when the event occurs on the topic |
Subscribes to a specific topic.
await topicsController.subscribe('chat/room1');| Parameter | Type | Description |
|---|---|---|
| topic | string | Topic to subscribe to |
| Returns | Promise | Resolves to true when subscription is complete |
Unsubscribes from a specific topic.
await topicsController.unsubscribe('chat/room1');| Parameter | Type | Description |
|---|---|---|
| topic | string | Topic to unsubscribe from |
| Returns | Promise | Resolves to true when unsubscription is complete |
Unsubscribes from all currently subscribed topics.
await topicsController.unsubscribeAll();| Returns | Promise | Resolves to true when all unsubscriptions are complete |
Refreshes all current subscriptions by unsubscribing and resubscribing.
await topicsController.regenerate();| Returns | Promise | Resolves to true when regeneration is complete |
// Initialize
const topicsController = new TopicsController(realTimeClient);
// Subscribe to a topic
await topicsController.subscribe('notifications');
// Listen for events on that topic
topicsController.on('notifications', 'new', handleNewNotification);
// Clean up when done
await topicsController.unsubscribe('notifications');
// Or unsubscribe from everything
await topicsController.unsubscribeAll();