Is it a bad idea to use AJAX to emulate a WebSocket?
1 27 Nov 2016 14:26 by u/Codewow
For example: a chat system that pings the server every couple of seconds to see if there's a new message.
I assume is bad for scaling, but could work for a smaller website.
Or am I completely wrong?
(Obviously I know little about AJAX and it's capabilities)
I'm going to be learning it though and I figured I should ask before getting greedy.
8 comments
2 u/mofukka 28 Nov 2016 16:26
Nah not if necessary, FB does the shit out of this. Ajax long poll requests are a common pattern in update / notification checking.
1 u/bbr 28 Nov 2016 03:09
Isn't AJAX primary usage to grab data from an API to prevent another http request? I don't think that can be classified as true 'real-time'
0 u/Codewow [OP] 28 Nov 2016 03:18
That's where my question comes from. Could I possibly emulate 'realtime' efficiently using AJAX. I don't need it to be realtime, but a close second is acceptable for my current needs.
1 u/bbr 28 Nov 2016 03:41
It depends on what you're building. If you simply want to give the user the ability to send updates to a backend without sending a full request cycle i.e. clicking "thumbs up" on a youtube video and having it update on the page, then yes, thats exactly what ajax is for. I assume you already know some javascript since you're asking about AJAX so I'll recommend angularJS or react as a framework.
If you're building 'real-time' chat you should look into web sockets.
0 u/Codewow [OP] 28 Nov 2016 03:47
real-time chat was just an example. Though I would love to work with Websocket over 'hacking' my way around AJAX to do the same thing I want to do. I just don't have the money to support a dedicated web host that allows websocket.
0 u/littul_kitton 29 Nov 2016 08:43
Several companies have virtual private servers (VPSes) for under $2/month. VPSCheap budget servers are an example -- US $11 for 6 months.
P.S. You get what you pay for.
0 u/technically_sound 29 Nov 2016 19:16
Websocket fallback is long polling but it's not efficient. Especially if it's polling every 1 or 3 seconds, but it does work in a pinch. Web sockets can be a pain to get working correctly, depending on your stack. Try to be polite about the length of polling, especially if users are on their mobile phone. Maybe detect if it's mobile and/or not wifi then lengthen the time for ajax polling.