V
V
Vitalik logvinenko2014-01-05 16:56:41
Node.js
Vitalik logvinenko, 2014-01-05 16:56:41

If such and such an event occurred in the database, how to tell it to such and such clients and perform such and such actions?

Hello. I decided to get acquainted with node.js, mongodb and websockets, in connection with which I started developing a small resource. A duel is created between several players, when a new person enters the duel, his id is recorded in the database. Now you need to tell the rest of the participants in this duel that a new player has appeared and, accordingly, show this information to them (in simple terms: if such and such an event occurred in the database, say it to such and such clients and perform such and such actions). I'm dabbling in the direction of sockjs but I can't figure it out until the end. Experience, alas, is not enough and stalled on such a problem. In general, please tell me who than can, how to solve problems of such a plan.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Shikanov, 2014-01-05
@Miren

As far as I understand, there are no rooms or any kind of routing out of the box in SockJS. Therefore, you will need to implement it yourself. The algorithm is something like this:
- a unique identifier is generated for each room
- clients in the room are assigned its identifier and a handler is assigned that subscribes to the corresponding pubsub channel in Redis.
- when connecting a new user, we push a message that a new user has connected to the room into the room channel (pubsub)
- handlers of each client receive a message and send it directly to the client
Alternatively, you can switch to something more functional, for example: faye or socket. io .

S
Sergei Chukhan, 2014-01-05
@street

It's not entirely clear why Redis is here? Without it, I think we can limit ourselves to JS tools:
1. Create an object where lists of rooms will be stored (names can be generated unique);
2. When a client connects, we enter information in the socket about the room being connected;
3. We transfer the identifier of the connected client to the required "room" into the object with the list of rooms.
To do this, you can implement a simple module in Node.JS. You can easily implement notifications for the desired room, because. You will already have a list of all connected clients in the room. The main thing is not to forget about timeouts and automatic unsubscribing from the room.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question