W
W
WAYNEDEV2020-02-23 19:39:52
JavaScript
WAYNEDEV, 2020-02-23 19:39:52

How to send data to a specific user via socket.io?

Hello everyone
In general, there is a game where users do various actions.
All actions are processed and data is transferred to the database of users, but I don’t know how to transfer exactly its data from the database through sockets and so that it displays the changes, if any
. need to show these changes in
real time Authorization done by jwt + passport.js
Please help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Shumov, 2020-02-23
@inoise

We store in memory (or cache) a "table" with connection and user associations. When connected, the table is replenished, when disconnected, the necessary records are deleted accordingly. When an event occurs, we look for all its connections and for each helmet the same message.

V
Vladimir, 2020-02-24
@HistoryART

When reporting from a specific socket, return the message to the socket, not to the server.
server:

io.on('connect', function(socket) {
  socket.emit('message','Учите документацию!');
});

client:
socket.on('message', function(data) {
  console.log(data);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question