-
Notifications
You must be signed in to change notification settings - Fork 10
Pub Sub System
ClusterWS Pub/Sub system is the only way to communicate between different clients client
<-> client
communication.
In this documentation we will show the only functionality of Pub/Sub system for more examples of client
<-> client
communication check Client to Client Communication guide.
...
let channel
socket.on('connect', () => {
// Subscribe to the channel (can be any channel you want)
channel = socket.subscribe('channel-name')
// Listen on messages to that channel
channel.watch((message) => {
// Execute code when receiving messages on the channel
})
// Publish any message you want
channel.publish('any-data-you-want')
// Unsubscribe from the channel
channel.unsubscribe()
})
...
also you can chain everything if you want
...
socket.on('connect', () => {
socket.subscribe('channel-name').watch((message) => {
// Execute code when receiving messages on the channel
}).publish('any-data-you-want')
})
...
in case if you want to get the channel from the socket (must be subscribed before) you can use getChannelByName
method
...
const channel = socket.getChannelByName('channel-name')
channel.publish('data-to-that-channel')
...
Note: You can subscribe to the channels only if the socket is connected to the server (not before that). To make sure that socket is subscribed you can do it on connect
event or on any other events you emit from the server.
💥 We would really appreciate if you give us stars ⭐ (on all our repositories):
For you to give the stars ⭐ is not hard, but for us, it is a huge motivation to work harder and improve the project. Thank you very much 😄.
1. Home
2. Installation and Configuration
5. Client to Client Communication
Other languages will be added later