Giter Club home page Giter Club logo

Comments (6)

goriunov avatar goriunov commented on May 18, 2024

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 .

from clusterws.

goriunov avatar goriunov commented on May 18, 2024

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

from clusterws.

carmas123 avatar carmas123 commented on May 18, 2024

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?

from clusterws.

goriunov avatar goriunov commented on May 18, 2024

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.

from clusterws.

goriunov avatar goriunov commented on May 18, 2024

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.

from clusterws.

goriunov avatar goriunov commented on May 18, 2024

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

from clusterws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.