Barbara Győri
Barbara Győri

Reputation: 1

How can I use socket io for just server client communication?

I would like to build a web page with interactive content.

I would like to use socket IO, but I've got a problem when I send two number from client to the server.

The server adds the two numbers and sends for every user (but I want just send back to the one user). I wouldn't like to store the users. So I would like to ask how can I build this example using NodeJS and Socket IO?

Upvotes: 0

Views: 45

Answers (1)

bcg07
bcg07

Reputation: 11

if you look into socket.io docs, you will find out that you can specify the key for each message you send, for instance:

io.on('connection', function (socket) {
  // specify a clientId for each client you have, you may define it at the moment the connection starts.
  socket.emit(`news:${clientId}`, { hello: 'world' });
});

And, on the client side, you have:

socket.on(`news:${myClientId}`, function (data) {
  console.log(data);
});

You may generate this id randomly by using many libraries, for instance Node-Forge.

Hope it helps! Feel free to ask further.

Upvotes: 1

Related Questions