Supporting Server Sent Events with Netty HTTP2 implementation
Asked Answered
S

1

7

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.

Shelleyshellfire answered 8/9, 2015 at 20:2 Comment(0)
S
4

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!

Shelleyshellfire answered 15/9, 2015 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.