Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Message from Server to specified client #37

Closed
carmas123 opened this issue Dec 18, 2017 · 6 comments
Closed

Message from Server to specified client #37

carmas123 opened this issue Dec 18, 2017 · 6 comments

Comments

@carmas123
Copy link

Hi, please can you explain me how can I send a message to a channel or a specified user from server?

I need to send to an user or many users a message or data when an event occur on server.

@goriunov
Copy link
Member

goriunov commented Dec 19, 2017

For sending message to a group of users or to a specific user you should use Pub/Sub System.
For example you want to send message to each user which are subscribed to the channel Global. So first you need to subscribe users, that thing you can do only from the client library as example will use Client-js:

// Connect socket
var cws = new ClusterWS({
    url: 'localhost',
    port: 80
})
// Listen on connection event
cws.on('connect', function(){
    let globalChannel = cws.subscribe('Global')

    // listen on messages which comes to the channel
    globalChannel.watch(function(msg){
          // print messages which you retrieve on the channel
          console.log(msg)
    })
})

This is example where you subscribe to the channel on connect event you can do it on any events you want (BUT YOU SHOULD DO IT AFTER SOCKET IS CONNECTED).

Ok you have done with client part, now you should send message to this channel from the server:

function Worker() { 
    const httpServer = this.httpServer
    const socketServer = this.socketServer
     
    .....

    // Listen on WebSocket connection
    socketServer.on('connection', (socket) => {
        // Publish to the channel after 1s
        setTimeout(()=>socketServer.publish('Global', 'hello world'), 1000)
    })
}

I have set timeout just to make sure that user will be subscribed to the channel on connect event before i publish an event Global. It mainly depends on the way you want to write your app.

Make sure that when you publish event you have users connected to the channel other vise event wont fire. Also you can socketServer.publish from every where in the Worker function and on any event or action on the server. Also dont forget that Worker is scaled across machines and the same publish event may fire on several machines so you should design server proprely.

With unique users you can do exactly the same thing but subscribe to the channel which is for example unique_user_id and then you can publish to this channel and only this user will retrieve data.

Also u can publish to the channel from client library check out documentation .

@goriunov
Copy link
Member

This is just an example, and it mainly depends on how you want to implement your app.

@carmas123
Copy link
Author

carmas123 commented Dec 19, 2017

This is good. But I don't know why I need to handle the connection with a timeout:
setTimeout(()=>socketServer.publish('Global', 'hello world'), 1000)

The socket is not really connected when the "on('connected')" event is emitted?

@goriunov
Copy link
Member

goriunov commented Dec 19, 2017

You don't have to use time out, it is just an example. In this example user subscribe to the channel on connect and we publish from server on connection too the problem is latency, publish calls before subscribe in this case (or at the same time) and that is why message wont be delivered because user is not completely subscribed.

The socket is not really connected when the "on('connected')" event is emitted?

No, it is completely connected but to subscribe you need to emit event to the server which will subscribe user to the channel. This event cause small latency.

@goriunov
Copy link
Member

goriunov commented Dec 19, 2017

However if you will connect two users and remove timeout, then you may notice that your first user will receive message when your second user will be connected. In that case your first user is connected and subscribed before second user is connected.

Because socketServer.publish sends data to all users which are subscribed to this particular channel.

@goriunov
Copy link
Member

You can find some details about sending messages and pub sub in this article.

“Real Time Chat with Node.js Cluster & Websocket (ClusterWS)” https://medium.com/clusterws/clusterws-chat-with-node-js-clusters-and-websockets-cb6c1224bd79

@goriunov goriunov closed this as completed Jan 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants