Scarass
Scarass

Reputation: 944

Dealing with data changes on server in single page web apps

Say that we're building a single page forum app. Many users can post new posts at any time. How can a single page app know that data on the server has changed (i.e. that new post has been added).

It could request new data from the server periodically but that's inefficient, in Firebase you sync the data very simply, you just add a callback function which is to be called when the data changes.

I'm not sure that's the way it is usually achieved in single page web apps (why not if it's not). This is the only library I have found that does something similar. But again, it's not exactly what I want. In that example, the server just notifies the client that the data has changed, but then the client has to request the data and wait for the response... which is less efficient than just receiving a JSON when data has changed.

When someone answers this question, I will see the answer immediately, no need to refresh the page. How does it do that, does it request the new data periodically, or does the server simply send the new data when there is a new data? Or does it just notify the client and the client than requests new data?

Upvotes: 0

Views: 230

Answers (1)

Kyle Richardson
Kyle Richardson

Reputation: 5645

Take a look at socket.io.

Socket.IO enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.

On the server you would setup a socket and use socket.emit(eventName[, ...args][, ack]) to emit an event that clients can listen for.

On the client you would setup a socket and use socket.on(eventName, callback) to update your client side application with the callback.

Upvotes: 1

Related Questions