AJAX/Reverse AJAX: Polling or Push?

痞子三分冷 提交于 2019-12-04 21:20:33

问题


Been studying a ton on AJAX because I'm making a real-time application out of Javascript and PHP. It needs to be able to update without refreshing the page and in real-time.

I tried polling the server with setInterval() but for it to be quick I had to have it every second. It seems to be using a ton of bandwidth though. It does work however and I have a plan with my hosting provider for 'unlimited bandwidth'. It seems like a lot of stress on the site though so I wanted to use a Push technology.

Learning how to setup Push was extremely difficult. From what I managed to figure out, you have to host some type of push server or comet server. I also looked into websockets, but there too you had to host it yourself. I don't understand how to do that when I do not own a server computer and there is no documentation on my hosting providers website about this.

Do specific hosting providers host push server for you? Is there a way to get a server without having to host it? It seems like a much better choice over polling, but at the same time it seems extremely confusing.


回答1:


Do specific hosting providers host push server for you? Is there a way to get a server without having to host it? It seems like a much better choice over polling, but at the same time it seems extremely confusing.

Yes, I work for Pusher who offer such a service. You can see a quickstart guide here. There are also other realtime hosted services that you could read up on.

I'd also recommend you have a look at:

  • My answer to How to Use Long Polling or Ajax Push in your Website - PHP focused
  • Using Comet with PHP
  • Implement Comet with PHP

My opinion, as expressed in the first link, is that PHP isn't a good technology for realtime web applications. You are best to either have different components that you use as your realtime web infrastructure. That should either be something self-installed or a hosted solution such as Pusher, who I work for. Why? PHP + Apache wasn't built for maintaining long running connections. There are PHP standalone WebSocket solutions you could look at, but you are 80% likely to be using Apache and 20% nginx which doesn't support WebSockets quite yet (but will soon). Even then, I'm not sure how they'll integrate with the standard PHP request/response application flow.



来源:https://stackoverflow.com/questions/11310518/ajax-reverse-ajax-polling-or-push

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!