Class WebSockets


  • public abstract class WebSockets
    extends Object
    WebSockets support for Ratpack.

    An example that broadcasts strings to a websocket every second:

    
     import org.reactivestreams.Publisher;
     import ratpack.test.embed.EmbeddedApp;
     import ratpack.core.websocket.WebSockets;
    
     import java.time.Duration;
    
     import static ratpack.exec.stream.Streams.periodically;
    
     chain.get("whatever", context -> {
       Publisher<String> stream = periodically(context, Duration.ofSeconds(1), i ->
         i < 5 ? i.toString() : null
       );
    
       WebSockets.websocketBroadcast(context, stream);
     });
     
    See Also:
    Writing WebSocket client applications
    • Constructor Detail

      • WebSockets

        public WebSockets()
    • Method Detail

      • websocketBroadcast

        public static void websocketBroadcast​(Context context,
                                              Publisher<String> broadcaster)
        Sets up a websocket that sends the published Strings to a client.

        This takes the place of a Streams.bindExec(Publisher) call.

        Parameters:
        context - the request handling context
        broadcaster - a Publisher of Strings to send to the websocket client
      • websocketByteBufBroadcast

        public static void websocketByteBufBroadcast​(Context context,
                                                     Publisher<io.netty.buffer.ByteBuf> broadcaster)
        Sets up a websocket that sends the published byte buffers to a client.

        This takes the place of a Streams.bindExec(Publisher) call.

        Parameters:
        context - the request handling context
        broadcaster - a Publisher of ByteBufs to send to the websocket client