All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.reactivex.netty.examples.http.sse.README.md Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
Overview
========

RxNetty supports HTML5 Server-Sent Events (SSE) streams. This feature is provided as a custom pipeline configuration
that includes SSE message encoding and decoding. For more information about SSE look
[here](http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#event-stream-interpretation).

Server side events are modeled with io.reactivex.netty.protocol.text.sse.ServerSentEvent class.

Running
=======

To run the example execute:

```
$ cd RxNetty/rxnetty-examples
$ ../gradlew runSseServer
```

and in another console:

```
$ cd RxNetty/rxnetty-examples
$ ../gradlew runSseClient
```

HTTP client
===========

Here is the snippet from [HttpSseClient](HttpSseClient.java):

```java
public List readServerSideEvents() {
    HttpClient client =
            RxNetty.createHttpClient("localhost", port, PipelineConfigurators.sseClientConfigurator());

    Iterable eventIterable = client.submit(HttpClientRequest.createGet("/hello")).
            flatMap(new Func1, Observable>() {
                @Override
                public Observable call(HttpClientResponse response) {
                    printResponseHeader(response);
                    return response.getContent();
                }
            }).take(noOfEvents).toBlocking().toIterable();

    List events = new ArrayList();
    for (ServerSentEvent event : eventIterable) {
        System.out.println(event);
        events.add(event);
    }

    return events;
}
```
The HTTP client is created with the predefined SSE pipeline configuration returned by
PipelineConfigurators.sseClientConfigurator() method. It injects SSE message
decoder in Netty's request processing pipeline, which converts HTTP reply body byte stream into
ServerSentEvent object stream. Calling HttpClientResponse.getContent method returns an Observable of
ServerSentEvent objects.


HTTP server
===========

Here is the snippet from [HttpSseServer](HttpSseServer.java):

```java
public HttpServer createServer() {
    HttpServer server = RxNetty.createHttpServer(port,
            new RequestHandler() {
                @Override
                public Observable handle(HttpServerRequest request,
                                               HttpServerResponse response) {
                    return getIntervalObservable(response);
                }
            }, PipelineConfigurators.sseServerConfigurator());
    System.out.println("HTTP Server Sent Events server started...");
    return server;
}

private Observable getIntervalObservable(final HttpServerResponse response) {
    return Observable.interval(interval, TimeUnit.MILLISECONDS)
            .flatMap(new Func1>() {
                @Override
                public Observable call(Long interval) {
                    System.out.println("Writing SSE event for interval: " + interval);
                    return response.writeAndFlush(new ServerSentEvent(String.valueOf(interval), "notification", "hello " + interval));
                }
            })
            ...
}
```

Server side SSE pipeline configuration is created with PipelineConfigurators.sseServerConfigurator() method.
Analogously to client counterpart, it injects SSE message encoder into Netty's response processing pipeline,
which converts ServerSideEvent object into text representation following HTML5 standard rules.
RxNetty application is released from dealing explicitly with message encoding/decoding and can use exclusively domain
objects instead.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy