What is difference between grpc and websocket? Which one is more suitable for bidirectional streaming connection?

懵懂的女人 提交于 2020-11-30 02:20:53

问题


I want to develop a client-server application in a bi-directional streaming manner.

what is more suitable technology for this, grpc or websocket?


回答1:


gRPC is not really the relevant part for comparison, it's that gRPC uses HTTP/2 which can certainly be compared to WebSockets.

https://www.infoq.com/articles/websocket-and-http2-coexist

This article outlines them quite well. Essentially, HTTP/2 is Client/Server with Server Push on the background, so you can make your request and simply stay on that connection listening for updates without the need for polling, for example.

Whilst WebSockets are not going away because of HTTP/2, they might not be considered necessary for use cases that center around "let me know when updates happen related to the thing I just did".




回答2:


gRPC is an API/Protocol on top of HTTP/2, so it is more relevant to compare HTTP/2 and Websockets.

Note: HTTP/2 Server Push is not relevant here either. That is a website optimization technique for cacheable (GET) resources.

Websocket and HTTP/2 support binary data frames with low overhead (a few bytes), however frames (whole payload) in Websocket is masked at sender and then unmasked at receiver. See What is the mask in a WebSocket frame?.

With HTTP/2 you can have multiple streams multiplexed over the same connection. This need to be handled by application developer or a library when using Websocket - if desired.

If your client is a browser, the answer to HTTP/2 or Websockets for low latency client to server messages may be relevant.



来源:https://stackoverflow.com/questions/46904674/what-is-difference-between-grpc-and-websocket-which-one-is-more-suitable-for-bi

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