Giter Club home page Giter Club logo

Comments (6)

jondubois avatar jondubois commented on May 18, 2024 1

The reason why you can't publish events directly from the SocketCluster master is because it is an anti-pattern - We should avoid putting any runtime logic in master - The master process isn't as resilient as worker processes so it's better to keep it lightweight (mostly good for configuration/bootstrap logic).

The best place to publish to an SC channel from outside is in the worker (worker.js) - The worker object which is passed inside the run() method has an id property which is the index of the current worker - There will always be at least one worker with id 0. So inside worker.js, you can have this code:

// By checking worker.id we can make sure that only one worker is handling a particular event
// So we don't get duplicates
if (worker.id == 0) {
  // ... Here we can do anything we want like listen for events from outside Node.js
  // For example, we can use the Node.js redis client https://github.com/mranney/node_redis 
  // to subscribe to specific channels from redis and publish data to 
  // SocketCluster channels by calling worker.global.publish('myChannel', dataFromRedis);
}

If you have a LOT of external data coming in from many different Redis channels (too much for a single worker process to handle), you could split responsibilities between multiple workers and make it so that each worker handles a specific subset of all Redis channels - This approach is more scalable.

from socketcluster.

jondubois avatar jondubois commented on May 18, 2024 1

@slidenerd

if I do worker.exchange.publish() from worker 0 and if a client is connected to worker 1 will they still get this data?

Yes, channels in SC are global so they are shared between all processes. If you use SCC, channels are also shared between all machines so it doesn't matter what process or machine the client is connected to.

from socketcluster.

jondubois avatar jondubois commented on May 18, 2024

EDIT I edited the post above since the information provided earlier about publishing from stores is not ideal - You should actually publish from workers - That way channel data will automatically get routed to the appropriate store which is responsible for that channel.

Note that in SC, if you have multiple store processes, each store will only be responsible for some of the channels - That way they share the work evenly.

from socketcluster.

jondubois avatar jondubois commented on May 18, 2024

I just started working on a Redis adapter which will allow hooking SC into Redis automatically to share channels.

from socketcluster.

jondubois avatar jondubois commented on May 18, 2024

I'm closing this issue. The optimal solution will be the Redis adapter - I opened a separate issue for it here: #38

In the meantime, the best way to interact with SC from external systems is by passing data to workers. You can use IDs to uniquely identify them.

from socketcluster.

slidenerd avatar slidenerd commented on May 18, 2024

@jondubois thank you for the detailed explanation, I have a similar case and a small doubt about the framework which I couldnt find in the documentation.

My workers are asymmetric and the way I am currently separating them is by doing what you did above

if (worker.id === 0){
//get data from 1st external websocket API
}
else if(worker.id === 1){
//get data from 2nd external websocket API
}

so if worker 0 and worker 1 are getting data from different APis. how do I let all the clients subscribe to this data

if I do worker.exchange.publish() from worker 0 and if a client is connected to worker 1 will they still get this data? My case happens to be where I want individual workers fetching data from different API sources but connecting clients to be able to get access to whichever data source they want regardless of which worker they are connected with

from socketcluster.

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.