0

Good day,

I am implementing a website that has to refresh data every seconds (there is a chat). I have made the website with setInterval and $.get but it is consuming a lot of requests on my server...(600 000 per day) So, I would like to know if the socket.io system would be more appropriate? or does it have the same effect as $.get requests?

lopata
  • 1,325
  • 1
  • 10
  • 23
  • see http://stackoverflow.com/questions/10377384/why-use-ajax-when-websockets-is-available. specifically the top answer. "If your application needs low-latency push events then this would be a factor in favor of WebSockets" – lhoworko May 28 '15 at 19:06
  • Depends on how many of your clients use browsers that support web sockets (if they don't, a polling fallback will likely be used, thus causing the same problem.) Chances are though that the majority of your clients do use browsers that support websockets. – Kevin B May 28 '15 at 19:10
  • What kind of "chat" is this? a shoutbox? for a simple shoutbox, it would be more time-effective to simply reduce the interval to 30 seconds, immediately reducing the number of requests from 600,000 to 20,000 with very little effort and without having a huge impact on the shoutbox. – Kevin B May 28 '15 at 19:25
  • @Kevin B : No it's a time real chat. And there are also other data that need to be display quickly – lopata May 28 '15 at 19:28
  • Switching to long-polling with a 30 sec timeout would have a similar reduction in requests while keeping the real-time aspect of it. – Kevin B May 28 '15 at 19:30
  • Websockets is certainly an option (I would definitely use websockets,) but the point i'm trying to make is you don't have to go to websockets to fix this problem. – Kevin B May 28 '15 at 19:31
  • yes thank you, but it's a chat commenting a live event, so the messages need to be display quickly – lopata May 28 '15 at 19:31
  • Yes, long polling would display a new message with the same speed that your current 1 sec interval does. web sockets on the other hand may do so even faster than that. – Kevin B May 28 '15 at 19:32
  • really? how? just change the setInterval to 30 000 ? – lopata May 28 '15 at 19:33
  • 1
    You would have to change your server-side code too so that instead of instantly returning a response, it would poll for new data every second until new data is available, or 30 seconds have passed. When new data is available, or 30 seconds have passed, it returns to the client, the client then sends another request that does the same. – Kevin B May 28 '15 at 19:34
  • Web sockets on the other hand would have no polling. Instead, when a new message happens, it is broadcasted to all the users. Far more efficient for a chat application. – Kevin B May 28 '15 at 19:35
  • oh ok I get it thanks – lopata May 28 '15 at 19:43
  • How do you make the php wait until it find new mysql messages? (post it as an answer maybe) – lopata May 28 '15 at 19:50
  • Another relevant answer here: http://stackoverflow.com/questions/29856618/cordova-sockets-pushnotifications-or-repeatedly-polling-server/29857040#29857040 – jfriend00 May 28 '15 at 22:13

0 Answers0