1

i am making a Web app using Socket.io with express.

I want to emit data to all the clients and all the clients can edit/update that data and that updated data will be updated in realtime(for all the clients).

Is this possible, without removing and updating whole data? And what if a new user joins the room and i want to show the data same as on the other clients

5
  • What does this: "Is this possible, without removing and updating whole data?" mean? You can call io.emit(someMsg, someData) from your server and it will send data to every connected client (on the top level namespace). What data you send and what the client does with it are completely up to your code. If you want send small incremental pieces of data that change, you can certainly do that. You will have to make the client smart enough to know what to do with that small piece of incremental data so it can update the client display accordingly. Commented Jan 27, 2020 at 4:42
  • I mean that on change, do i need to send the whole data to server and on the client side (i need to remove old data and post the whole data again) ? Commented Jan 27, 2020 at 4:45
  • Is there any method or something which handles the changes? Commented Jan 27, 2020 at 4:46
  • There's nothing built into node.js, socket.io or express. You have to do that yourself. We could help you a lot more specifically if you showed us exactly what it is you're trying to update and what the data looks like in the client and what types of incremental updates you would send. Commented Jan 27, 2020 at 4:47
  • 1
    As for a new user joining, I would assume that your server is responsible for keeping a copy of the current set of data so any new user can be sent the whole set of current data (at least that's usually how it works). Commented Jan 27, 2020 at 4:48

1 Answer 1

1

Is this possible, without removing and updating whole data?

Yes, it is possible.

Socket.io is just a transport that lets you send data between client and server. With code such as:

io.emit('someMsg', someData)

You can broadcast data to all clients connected to the default namespace.

If you want to send incremental updates to each of your connected clients, then you have to manufacture that incremental data on your server, put it into an object or array and then broadcast that to your clients.

It will then be up to the clients themselves to receive that data, process it and update their own client display - presumably by changing the DOM to insert, delete or modify data on screen.

Neither node.js, socket.io or express have any built-in mechanisms for creating the incremental packets of data or updating the display. That would be up to you to do.

And what if a new user joins the room and i want to show the data same as on the other clients

Generally, it is the server's responsibility to know how to generate the current data set for any new user that enters. It may be able to do that from scratch with some set of queries or it may have to accumulate the data that has been built over time so it can send that to any new users (or even any existing user that just hits refresh).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.