6

now I am learning how to build single page web applications with node.js. When I come to the communication part between server and client, I am confusing about the difference between http post request and web socket communication

Here is my understanding:

  • Web socket like socket.io is asynchronous communication while POST is synchronous communication method.
  • socket.io is mainly used in dealing with real-time task like html games or dynamic online chatting, or boradcasting some informations.
  • for validating forms when some user sign up for certain website, it is really good to check instantly if the user name is already been taken or not, I think it is using socket.io to achieve that.
  • for example in a login page, after I press the username and password, then press 'login' button, I could just use emit things in socket.io to transfer a JSON file with these information. when we checked these information and send back a flag string like 'true' or 'false ' to indicate if the login is success or not.

So my question is:

  1. Why we still use HTTP POST for login and signup ?
  2. Is socket.io better than HTTP POST ?
  3. Why not replace HTTP POST with socket.io ?

P.S for some large files, I have not go into these parts, so I have no idea.


Here is some additions:

After reading a similar question I find I seem to assume that WebSocket is a replacement for HTTP. It is not. It's an extension.

I think this asnwered my third question Why not replace HTTP POST with socket.io.

Community
  • 1
  • 1
Haven
  • 7,808
  • 5
  • 25
  • 37
  • http post with login's and signup's is useful because it will not store the url in browsers history or bookmarks, so this way via a post evil people can't intercept the user/password details via history or bookmarks but via a GET it is possible. I have done some apps in node.js with express.io and I still use post's for this. Hope that clears it up for that part of the question. – GiveMeAllYourCats May 13 '14 at 23:25
  • 1
    @MDG thanks for this explanation about comparison between POST and GET, what about the comparison between POST and using socket.io when user signing up? – Haven May 14 '14 at 09:37
  • You mean sending signup/login info over a socket.emit ? or letting node.js handle data from a post (or socket.emit) and create a account/confirm authentication? Kind of confused what you mean :) – GiveMeAllYourCats May 14 '14 at 12:09
  • @MDG Hi, I mean use `emit` to send the data like username and password, and server use node js `socket.on` to handle data and confirm auth. – Haven May 14 '14 at 12:56
  • The difference is just that you not using http but a websocket for authentication. No problem in doing this. Although I normally just use a post and catch that in nodejs, but depends on your situation really. – GiveMeAllYourCats May 14 '14 at 14:46

0 Answers0