Ajax vs Sockets08 Nov 2014
A friend asked me following questions so I replied with my understanding about sockets and ajax. It might be useful for others too:
###Sockets vs Ajax.. what are non obvious differences?
First websockets are not HTTP, http is stateless, classics such as sessions, cookies, put, push, file uploads are done via http. Http is simpler more common easier to understand protocol, ajax is just an http request.
Coding is simpler and any modern server would support it, you don’t need special language/code to do ajax. Sockets have dependencies and learning curve and maintainability nightmares
###Why exactly do we need sockets if everything can be achieved by ajax?
Not everything can be achieved without sockets… as a rule of thumb: Always use sockets when there are multiple clients and an act by one client affects ui of other client… Long polling using ajax on classic servers such as apache or nginx is very costly
Everything can be achieved with sockets that can be done by ajax, but.. lets say I want to log user clicks on page for analytics, I do it via sockets it will keep the socket open until page closes (I can close after request but that kills the point of a socket). Means even though there’s no need to maintain connection I still do maintain it. Imagine I have 1000 users reading different pages on my site I will have to maintain 1000 connections. On other hand, ajax requests come and finish. 1000 connections are worth it, if connection 3rd’s state would be altered by connection 998ths actions.
###Why are there so many frontend frameworks like backbone, ember and angular supporting REST and not sockets
They support REST, REST is really good way of developing modern web apps. Ideally, REST would be HTTP based, but with some tweaks even sockets can be used. Check https://github.com/scttnlsn/backbone.io . Take an example, backbone sends this request on REST via ajax:
With sockets we might send this message:
We can then write a middleware in server code for sockets which strips out method and passes it as http method to classic code and returns response. Easy peasy.. so js frameworks dont relate with ajax or socket io they are meant to organize and help u write better code.
###My Personal Preferences
I have similar feelings for ruby, but I doubt I am wrong in that but anyways, that’s for another time….