Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supporting Server Sent Events with Netty HTTP2 implementation

I am using Netty 4.1-Beta6 version.

I want to support the use case where the HTTP2 server should be able to push events to the HTTP2 client on an existing connection - this could be an alarm or timer event from the cloud which needs to be propagated to the client.

  1. I am thinking of using 'Server Sent Event' feature - is it possible to do this with HTTP2 in Netty, if so, how? Should I keep a http2 stream open by sending data frames with 'final frame' flag set to false? When I try this, what I observe is that the content gets buffered. The data frame doesn't reach the client as and when I write. I am using the DefaultHttp2Encoder. I tried setting the 'Transfer-Encoding' header to 'chunked' too.

  2. Related question - does HTTP2 allows bi-directional data frames once the stream is in 'open' state? The idea is that the server should be able ask data from the client and client should be able to respond with the data in the same stream (reversal of client/server role once the stream is established). Is this possible?

Thanks in advance for the help.

like image 321
Sathish Avatar asked Sep 08 '15 20:09

Sathish


1 Answers

I played with Netty bit more. Here is what I found for the 2 questions above.

  1. Both the client and server can keep the stream in 'open' state by sending 'endOfStream' as false when they send the header/data frames. In order to avoid the buffering of data on the server side, I had to invoke flowController.writePendingBytes() followed by ChannelHandlerContext.flush()'.

I have uploaded my sample here - https://github.com/skssfo/http2

  1. Yes, the client and the server can keep the stream open and send data frames independent of each other.

I am playing with Netty for the first time, it is very cool. Nice job Netty team!

like image 196
Sathish Avatar answered Oct 18 '22 00:10

Sathish