Comments (6)
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.
This is just an example, and it mainly depends on how you want to implement your app.
from clusterws.
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.
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.
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.
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)
- Is it possible create different (non clone) processes? HOT 4
- Close/Stop/Kill the server HOT 1
- How to determine which process current code is running in. HOT 7
- Having interest in translate document to zh-CN HOT 7
- Plz add `upgradeReq` argument, let it back HOT 1
- Can I store a dictionary in memory accessed from each workers? HOT 4
- How to upload files? HOT 4
- Publish to channel but only to some users HOT 5
- Callback in verify connection middleware HOT 6
- ws / wss proxy with nginx HOT 7
- How to send msg by userid connection? HOT 1
- Del
- Websockets inside a cluster, ok in production? HOT 2
- Callback issue on verifyConnection middleware HOT 12
- benchmark & does it work on browser too ? HOT 2
- uws not compiled for node 7.2
- How to see the list of channels a client is subscribed to HOT 3
- Don't understand what .processMessage is for? HOT 5
- Middleware.onPublishIn ? HOT 1
- TypeError: this.options.logger.warning is not a function
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 clusterws.