最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to implement long polling for React + axios - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jun 26, 2018 at 13:54 user7637745 9852 gold badges14 silver badges27 bronze badges asked Apr 17, 2017 at 1:11 MaklausMaklaus 6882 gold badges18 silver badges42 bronze badges 2
  • 6 Related answers: Long-polling vs websocket and Ajax vs Socket.io. 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 Commented Apr 17, 2017 at 1: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 Commented Feb 15, 2019 at 10:42
Add a ment  | 

1 Answer 1

Reset to default 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 pares to Websockets, read Alex Recarey's answer to "WebSockets vs. Server-Sent events/EventSource" in SO.

发布评论

评论列表(0)

  1. 暂无评论