综合技术

Socket.io vs AJAX Use cases

微信扫一扫,分享到朋友圈

Socket.io vs AJAX Use cases
0

Background:I am building a web app using NodeJS + Express. Most of the communication between client and server is REST (GET and POST) calls. I would typically use AJAX XMLHttpRequest like mentioned in https://developers.google.com/appengine/articles/rpc
. And I don’t seem to understand how to make my RESTful service being used for Socket.io as well.

My questions are

  • What scenarios should I use Socket.io over AJAX RPC?
  • Is there a straight forward way to make them work together. At least for Expressjs style REST.
  • Do I have real benefits of using socket.io(if websockets are used — TCP layer) on non real time web applications. Like a tinyurl site (where users post queries and server responds and forgets).

Also I was thinking a tricky but nonsense idea. What if I use RESTful for requests from clients and close connection from server side and do socket.emit()
.

Thanks in advance.

Problem courtesy of: rda3mon

Solution

Your primary problem is that WebSockets are not request/response oriented like HTTP is. You mention REST and HTTP interchangeably, keep in mind that REST is a methodology behind designing and modeling your HTTP routes.

Your questions,
1. Socket.io would be a good scenario when you don’t require a request/response format. For instance if you were building a multiplayer game in which whoever could click on more buttons won, you would send the server each click from each user, not needing a response back from the server that it registered each click. As long as the WebSocket connection is open, you can assume the message is making it to the server. Another use case is when you need a server to contact a client sporadically. An analytics page would be a good use case for WebSockets as there is no uniform pattern as to when data needs to be at the client, it could happen at anytime.

  1. The WebSocket connection is an HTTP GET request with a special header requesting the server to upgrade
    it to a WebSocket connection. Distinguishing different events and message on the WebSocket connection is up to your application logic and likely won’t match REST style URIs and methods (otherwise you are replication HTTP request/reply in a sense).

  2. No.

Not sure what you mean on the last bit.

Solution courtesy of: Tj Krusinski

Discussion

I’ll just explain more about when you want to use Socket.IO and leave the in-depth explanation to Tj there.

Generally you will choose Socket.IO when performance and/or latency is a major concern and you have a site that involves users polling for data often. AJAX or long-polling is by far easier to implement, however, it can have serious performance problems in high load situations. By high-load, I mean like Facebook. Imagine millions of people loading their feed, and every minute each user is asking the server for new data. That could require some serious hardware and software to make that work well. With Socket.IO, each user could instead connect and just indefinitely wait for new data from the server as it arrives, resulting in far less overall server traffic.

Also, if you have a real-time application, Socket.IO would allow for a much better user experience while maintaining a reasonable server load. A common example is a chat room. You really don’t want to have to constantly poll the server for new messages. It would be much better for the server to broadcast new messages as they are received. Although you can do it with long-polling, it can be pretty expensive in terms of server resources.

Discussion courtesy of: Michael Yoon

This recipe can be found in it’s original form on Stack Over Flow
.

阅读原文...


微信扫一扫,分享到朋友圈

Socket.io vs AJAX Use cases
0

Node.js Recipes

崔永元称有人微信冒充自己 微信:已封号处理

上一篇

Trump Insists Raking Would Help Stop Forest Fires; Twitter Reels

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

Socket.io vs AJAX Use cases

长按储存图像,分享给朋友