Is there any need/advantage of calling an api using the http client service when using websockets in angular?

末鹿安然 提交于 2020-01-15 14:56:07

问题


I am trying to learn how to use socket.io by building a simple chat app using the socket.io library and MEAN stack.

From looking at some open source projects(like this one) I have seen that the client, mainly when executing the chat logic, communicates with the server via websockets and not the http client service provided by angular.

Does this mean that when using web-sockets for real time updates there is no need to use http to communicate with the server?


回答1:


Most assuredly not. It is not a matter of angular though, it's just choosing the right tool for the right thing.

Long story short:

  • If it is a request/response model, then use http. Why?
    1. Because it's easier to handle. Proxies, dns and load balancers need no extra configuration to handle. Web Sockets do.
    2. You have already setup 1 so it's not a problem. How will you handle caching, routing, gziping, SEO and all the out of the box stuff the http protocol and rest-apis handle? Everything you build, all communications will need their own security consideration, design patterns etc.
    3. How will you handle the stateful nature of web sockets? They are currently supporting only vertical scaling, whereas rest apis scale both horizontally and vertically.

If you really need a full duplex communication (there is server push only without sockets), then you should limit web socket usage to the cases where you really need it.

Even in this case, go through a framework like signalR. All modern browsers support websockets but a lot of users still don't have browsers that do support them. SignalR falls back to long polling in those cases. If you use it on all cases, imagine what would happen if you use such a browser and you apply long polling for each request.

I could go on, but I think you get the meaning.



来源:https://stackoverflow.com/questions/58654499/is-there-any-need-advantage-of-calling-an-api-using-the-http-client-service-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!