(Quick Reference)

11.2.1 Server Sent Events

Version: 3.2.4

11.2.1 Server Sent Events

Server-sent events (SSE) is a technology where a browser receives automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is standardized as part of HTML5 by the W3C.

The RxJava plugin adds support for SSE to Grails making it simple to write controllers that maintain continuous non-blocking communication with a JavaScript client.

For example:

def index() {

    rx.stream { Subscriber subscriber -> (1)
       for(i in (0..5)) {
           if(i % 2 == 0) {
               subscriber.onNext(
                   rx.render("Tick") (2)
               )
           }
           else {
               subscriber.onNext(
                   rx.render("Tock")
               )

           }
           sleep 1000 (3)
       }
       subscriber.onCompleted() (4)
   }
}
1 Call the stream method passing a closure that accepts an rx.Subscriber to start sending events
2 Emit a one or many items using onNext
3 Call sleep to simulate a slow request
4 Call onCompleted to complete the request

For more detailed instructions on how to use SSE and the RxJava plugin see the user guide documentation for the plugin.