Comments (13)
Yes but you should keep in mind that a user could have multiple sockets open (on different browser tabs for example).
There are multiple ways to do what you want:
- If you only intend to ever have 1 worker process, then on the client you can simply emit the message to the server, then you can handle that event from inside your worker, then get a reference to the target client socket using the
scServer.clients
property here: http://socketcluster.io/#!/docs/api-scserver and then emit the message to that socket directly. Note that this approach is not scalable - As soon as you add a second worker, half of the sockets will be hosted by a different worker than the one which is handling the message so it won't work. This approach is not recommended. - You can setup backend-only channels using the
worker.exchange
object here: http://socketcluster.io/#!/docs/api-scworker. With this approach; whenever a new socket becomes connected, you can create a new channel from the server which is based on that socket's ID and subscribe to it (E.g.worker.exchange.subscribe('someSocketId123').watch(handlerFunction)
) - The handlerFunction can then just capture all messages published to that channel and forward them to the appropriate client socket usingsocket.emit(...)
. Your server logic will be responsible for listening to events from clients usingsocket.on(eventName, handler)
and relaying them to the appropriate backend channel usingworker.exchange.publish('someSocketId123', message)
. You may want to set theallowClientPublish
config of SC tofalse
(see http://socketcluster.io/#!/docs/api-socketcluster) so that only the server can publish to channels. The idea of this approach would be to setup all channels on the backend only - That way clients only communicate to the server usingsocket.emit(...)
and by listening to events usingsocket.on(...)
. Unlike the first approach, this one can scale quite well, but it means you have to manage channels on the backend (you have to remember to unsubscribe and destroy() the channel when the socket disconnects for example).
I think handling the channels on the frontend is generally a better/simpler approach overall but I understand that every case is different.
from socketcluster-client.
Thanks a lot for such a detailed explanation!
The first approach is exactly what I was searching for, but I see it is not scalable unless we find a way to notify all workers, and the one who handle such client id will emit the message (which I think is a worth direction to explore).
What concerns me in the second approach is the performance. Having thousands of users means handling thousands of channels (additionally to the regular ones) just in case some users may receive some messages once in a while. Maybe I am mistaken and pub/sub is fast enough to ignore it?
from socketcluster-client.
Channels in SC are pretty efficient, you can have many thousands of channels. The main problem would be if they all tried to subscribe at the same time (which could happen after a worker crash). But you can tweak the reconnect options to add randomness to the reconnect delay in socketcluster-client.
from socketcluster-client.
Thanks a bunch! It's awesome to have such a complex functionality done in just few lines of code. SocketCluster is a brilliant and handy piece of software!
In case someone will follow this instruction, we have also not to forget to set socketChannelLimit
to a higher value, as we will have maximum 1000
users by default. Maybe is there a way to set it to unlimited?
from socketcluster-client.
@zalmoxisus Note that there was an issue with the socketChannelLimit option in SC v4 - Basically the limit wasn't enforced. This was fixed in v4.2.1.
If you don't want any limit, you can set it to null
, 0
or Infinity
.
from socketcluster-client.
hello,I think this is not wise way to implement a chat room like Telegram or Wechat with socketcluster.You will have millon of channels.How possible you have not only million its billion of channels?Pls just have a look your whats app or Telegram there is channels with 1 millions subscriber.Everytime they come and need to communicate with huge of channels.its impossible.
from socketcluster-client.
Because you understand better just refer to your whats app and look you want to implement this by Socketcluster.forst Socketcluster advise make channel and destroy them :
1- It means you never have all people inside your chat room anytimes you need to access need to make channel and subscriber its huge of access
from socketcluster-client.
@Kamelia2000 Each SC channel can have millions of subscribers. Creating a channel in SC is extremely cheap so yes you can also have millions of them too. I don't think there would be any issues implementing something like Telegram or Wechat with SC. You can just keep adding more machines to handle more channels and users.
from socketcluster-client.
Hello thank you for your email.The only problem is Pub/Sub is not possible by SC.So we need to find some platform such as Kafka work beside SocketCluster So the jobs related to Pub/Sub handle by Kafka have use over three subscriber and others realtime handle by SC.It means for tiny and small subscriber maximum 5 use Socket cluster and rest of huge use Kafka or any others.One of the biggest problem if a worker Crashed we should find some way to handle concurrency and ingestion go to build another worker and its really danger.Imagine 100,000 users in Worker A if all of them Crashed going to move another I am sure because of Low polling in Kuber will encounter problem and make huge of error and request to do. Kamelia
from socketcluster-client.
In case of worker crash, the clients should not all reconnect at the exact same time. The SC JavaScript client has a random delay which can be configured. This randomness ensures that client reconnections are spread out over a certain period of time. Also, problems like this can simply be handled by adding more machines.
from socketcluster-client.
So whats your suggestion for Pub/Sub and Group chat in enterprise? Could you tell me how possible to connect Pub/sub inside this system and connect to cluster ? Because as i understand Socketcluster is not suitable for Pub/Sub
from socketcluster-client.
@Kamelia2000
Pub/Sub: https://socketcluster.io/#!/docs/full-stack
Clustering: https://github.com/SocketCluster/socketcluster/blob/master/scc-guide.md
from socketcluster-client.
I would like to know whwther possible to use Socketcluster for Geolocation ? Kamelia
from socketcluster-client.
Related Issues (20)
- socketCluster client on the backend
- Increases temperature on ios devices HOT 2
- TypeError: cb is not a function HOT 8
- Update release Page HOT 1
- Not able to run the client on the office-js in excel desktop HOT 2
- Subscribe method causes error in IE11 HOT 9
- TypeError: _iterator[Symbol.iterator] is not a function || For await of loop not working on Mobile (React Native) HOT 2
- Please bump version of remote socketcluster-client
- Remove gulp, and babel dependencies
- Can this client be used in non browser environment ?
- [docs] Add description about `autoReconnectOptions` option
- Unable to resolve as a es module
- Need help for implemeting a real-time chat HOT 1
- Incorrect disconnect code HOT 1
- socketcluster-client with protocol v1 HOT 1
- How to use "subscribeWildcard" instead of "subscribe" ? HOT 1
- How we add headers in socketclusterclient HOT 5
- I got TypeError: Object is not async iterable error while trying to listen to message event in React Native app
- Docs & readme incorrect on "rejectUnauthorized" HOT 3
- eventObject timeout has a timeout set when it's not expecting a response
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-client.