89

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

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

prashant sindhu
  • 1,231
  • 1
  • 13
  • 21
  • 1
    Well, gRpc good for bidirectional streaming, gRPC basically runs on HTTP/2 where streaming of data in binary format so it accelerates speed data flow. I think grpc plays better then web socket for bi directional stream of data. – Basavaraj Nov 21 '17 at 09:42
  • 1
    While this is being discussed [Here is an article](https://blog.idrsolutions.com/2018/07/rest-vs-grpc/) that is not directly relevant to this comparison but informative to read. – RC0993 Feb 28 '19 at 13:23

2 Answers2

103

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".

Phil Sturgeon
  • 29,768
  • 12
  • 74
  • 117
  • 6
    that is not just a _really nice_ document, it is **must read**! Thank you for the provided link! – maxkoryukov Mar 23 '18 at 16:05
  • Mayby you know the answer: https://stackoverflow.com/questions/62673508/how-web-push-protocol-works – zolty13 Jul 03 '20 at 08:27
  • Is it just the confident tone of this answer driving upvotes? "gRPC is not really the relevant part for comparison" - why not? gRPC **is** allowing bi-directional communication. "WebSockets ... might not be considered necessary" - again why? The answer goes into different topic than the question asks in its present form. – Mike Oct 29 '20 at 11:45
  • gRPC is built on top of HTTP/2, so the comparison between WebSockets and HTTP/2 is more appropriate. The article explains that HTTP/2 can take care of some of the use cases that WebSockets were often implemented to solve, but doesn't solve all of them. I felt like the answer and the article were both clear. – Phil Sturgeon Oct 29 '20 at 16:22
20

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.

Jonas
  • 97,987
  • 90
  • 271
  • 355