@rxstack/channels
v0.2.0
Published
RxStack Channels Component
Downloads
9
Readme
The RxStack Channels
With channels component you'll be able to create channels, add/remove connections (instance of
EventEmitter
) and send messages. The component is intended to be used with socketio or any other realtime framework.
Some examples where channels are used:
- Real-time events should only be sent to authenticated users
- Users should only get updates about messages if they joined a certain chat room
- Only users in the same organization should receive real-time updates about their data changes
- Only admins should be notified when new users are created
Installation
npm install @rxstack/channels --save
Usage
Channel manager
The channel manager is responsible for creating, merging and destroying channels.
// instance of channel manager
const channelManager = new ChannelManager();
// gets or creates a channel if does not exist
const adminsChannel = channelManager.channels('admins');
// or gets or creates merged channels, name of the merge channel would be admins_moderators
const mergeChannels = channelManager.channels('admins', 'moderators');
// destroys the channel
channelManager.removeChannel('admins');
// destroys merged channel
channelManager.removeChannel('admins_moderators');
// destroys all channels
channelManager.reset();
Channels
A channel is an object that contains a number of connections.
It can be created via channel manager
and allows a connection to join or leave it.
// ...
// gets or creates a channel if does not exist
const adminsChannel = channelManager.channels('admins');
// a connection
const connection = new EventEmitter();
// joins a channel
adminsChannel.join(connection);
// leaves a channel
adminsChannel.leave(connection);
// clears all connection
adminsChannel.reset();
Connections
A connection is an object (EventEmitter
) that represents a real-time connection.
It is the same object as socket in a Socket.io. You can add any kind of information to it.
License
Licensed under the MIT license.