Comments (6)
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.
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.
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.
I just started working on a Redis adapter which will allow hooking SC into Redis automatically to share channels.
from socketcluster.
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.
@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)
- Worker is leaving instance repeatedly and reconnects after a while HOT 1
- Is it possible to merge publish messages? HOT 1
- SocketCluster version 16.0.1 SCC problem in minikube k8
- JWT token : invalid signature HOT 1
- `socketcluster create` screws up app name HOT 2
- badSocketAuthToken event not triggering HOT 2
- Unused dependencies HOT 1
- Bump Docker image HOT 1
- Socket connection gets timed out unexpectedly HOT 3
- Potential security issue HOT 5
- any way to send the missing socket update to client after network restore HOT 3
- We unable received the middleware sending error message from socket client HOT 2
- Has anyone tried to deploy socket cluster on Azure Container apps? HOT 1
- socket.on is not a function HOT 3
- BadConnectionError - socketcluster-client when requesting from server side. It works fine if the request is issued from the browser. HOT 3
- Callbacks scale much more than async/await approach HOT 2
- Namespace in channels HOT 3
- Is there any CPU/Memory Resources requests / limits recommendation?
- Socketcluster 16.x stopped working due to underlying library update HOT 1
- socketcluster compliance HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from socketcluster.