0

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?

1
  • but the broadcast method is not good for me because send the message everyone else except me.but I want to send just a server and want to send the data for me back I don't want to send the data everyone. Commented Mar 15, 2019 at 13:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.