-
Notifications
You must be signed in to change notification settings - Fork 7
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.
func onConnect() {
/**
Subscribe to the channel (can be any channels you want)
*/
let channel = webSocket.subscribe("any-channel-name")
/**
Listen on messages in that channel
*/
channel.watch { (data) in
// execute code when receive any messages on the channel
}
/**
Publish any message you want
*/
channel.publish(data: "any-data-you-want")
/**
Unsubscribe from the channel
*/
channel.unsubscribe()
}
func onDisconnect(code: Int?, reason: String?) {
}
func onError(error: Error) {
}
- 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
func onConnect() {
webSocket.send(event: "event-name", data: "any-data")
}
func onDisconnect(code: Int?, reason: String?) {
}
func onError(error: Error) {
}
// Server
wss.on('connection', (socket) => {
socket.on('event-name', (message) => {
wss.publish('any-channel-you-want', 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 second method to send message to the client from another client.
// Client which receives message
// note that 'xyz' should be unique id for each user
func onConnect() {
webSocket.subscribe("xyz").watch { (data) in
print(data)
}
}
func onDisconnect(code: Int?, reason: String?) {
}
func onError(error: Error) {
}
// Client which sends message
func onConnect() {
webSocket.send(event: "send-to-friend", data: "{receiverId: 'xyz', message: 'any-data-you-want'}")
}
func onDisconnect(code: Int?, reason: String?) {
}
func onError(error: Error) {
}
// 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) this will motivate us to work harder. Thank you very much.