-
Notifications
You must be signed in to change notification settings - Fork 10
Client to Client Communication
Basically, there are 2 ways to communicate between clients and both of them use Pub/Sub System.
- The first way does not require any server coding (all your logic is saved in front-end application). You
subscribe
,watch
andpublish
from the front-end application.
socket.on('connect', () => {
// Subscribe to the channel (can be any channels you want)
channel = socket.subscribe('channel-name')
// Listen on messages in that channel
channel.watch((message) => {
// Execute code when receiving any messages on the channel
})
// Publish any message you want
channel.publish('any-data-you-want')
})
- The second way is to publish messages from the server, and not from the front-end application, this does not require user to be subscribed to the channel.
// Client
socket.on('connect', () => {
socket.send('event-name', 'any data')
})
// Server
wss.on('connection', (socket) => {
socket.on('event-name', (message) => {
wss.publish('any channel you want to publish', message)
})
})
as you can see in second example all logic of publish
moved to the server, also the second method gives you the ability to publish messages even if the client is not subscribed to the channel, plus client does not have to know the name of the channel.
The last part I wanted to mention about is specific client communication, like what if you want to send a message to a specific client, and not to the group. As you may notice across all documentation we talk that you can communicate with users only trough Pub/Sub System.
So there you go, you just have to create a unique channel (can be user_id, name, or email) for each client and use onsubscribe
middleware (check server documentation for that) on the server to allow only this particular client to subscribe to this unique channel (it is important that no one else will be able to receive any messages which are addressed to this particular client).
Then you can use the second method to send a message to the client from another client.
// Client which receives message
// note that 'xyz' should be a unique id for each user separately
socket.on('connect', () => {
socket.subscribe('xyz').watch((message) => {
console.log(message)
})
})
// Client which sends message
socket.on('connect', () => {
socket.send('send-to-friend', {receiverId: 'xyz', message: 'any-data-you-want'})
})
// Server
// onsubscribe middleware implementation (find in server documnetation)
wss.on('connection', (socket) => {
socket.on('send-to-friend', (message) => {
wss.publish(message.receiverId, message.message)
})
})
How you get receiverId
on front-end you have to figure that out by your self as there many different ways of doing it.
There are also ways to implement this logic differently but make sure that you sue Pub/Sub system to implement client
=> client
communication.
Note that above code is just an example which shows you how everything works.
💥 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