19

I'm using React + axios to talk to the API from the client side. I'm a newbie in JavaScript.

How would I implement long polling so I get near real-time updates on a web page?

Is there a better way to do real-time updates on the page, when backend is a JSON REST API? Should I look into using WebSockets or server side events or long polling is fine?

user7637745
  • 965
  • 2
  • 14
  • 27
Maklaus
  • 538
  • 2
  • 16
  • 37
  • 6
    Related answers: [Long-polling vs websocket](http://stackoverflow.com/questions/41198368/long-polling-vs-websocket-when-expecting-one-time-response-from-server-side/41203047#41203047) and [Ajax vs Socket.io](http://stackoverflow.com/questions/30319618/ajax-vs-socket-io/30334848#30334848). For regular real-time updates to a web page, I know of no situation where polling is more efficient than a continuous webSocket or socket.io connection. – jfriend00 Apr 17 '17 at 01:20
  • The question in your title and the question you ask at the end are different. Could you please update the post with a single question so someone can attempt to answer the right question. – Tom Feb 15 '19 at 10:42

1 Answers1

2

There is another, potentially better way for your use-case: Server-Sent Events.

SSE, in a nutshell, is a simple GET request to the server from the client - except that the server doesn't close the connection after it's done processing the request. Instead, the HTTP connection is left open and the server is able to write data multiple times to the client, which appear in real-time.

For more info on how SSE compares to Websockets, read Alex Recarey's answer to "WebSockets vs. Server-Sent events/EventSource" in SO.