Rick

Rick
Rick

Monday, February 16, 2015

Using QBit microservice lib's WebSocket support - JSON, WebSocket, Microservice

Using QBit microservice lib's WebSocket support



QBit microservice lib comes with a WebSocket lib that is geared towards delivering JSON.

It is very easy to use. It uses Java Lambdas so you can just implement the parts of the WebSocket that you care about. Ease of use and fluency is the name of the game, but it is also terse and readable. You can start a server and a client in a single main method and fool around. :)


To test it out, let's start up a WebSocket server, it will only take a few lines of code.

Create an HTTP server

        /* Create an HTTP server. */
        HttpServer httpServer = httpServerBuilder()
                .setPort(8080).build();

Setup server WebSocket support

        /* Setup WebSocket Server support. */
        httpServer.setWebSocketOnOpenConsumer(webSocket -> {
            webSocket.setTextMessageConsumer(message -> {
                webSocket.sendText("ECHO " + message);
            });
        });

Start the server

        /* Start the server. */
        httpServer.start();


Now let's start up the client. And we can send/receive messages form our WebSocket server.

Setup the WebSocket client

        /** CLIENT. */

        /* Setup an httpClient. */
        HttpClient httpClient = httpClientBuilder()
                .setHost("localhost").setPort(8080).build();
        httpClient.start();

Client WebSocket

        /* Setup the client websocket. */
        WebSocket webSocket = httpClient
                .createWebSocket("/websocket/rocket");

        /* Setup the text consumer. */
        webSocket.setTextMessageConsumer(message -> {
            System.out.println(message);
        });
        webSocket.openAndWait();

        /* Send some messages. */
        webSocket.sendText("Hi mom");
        webSocket.sendText("Hello World!");

Output


ECHO Hi mom
ECHO Hello World!

Now stop the server and client. Pretty easy eh?
        Sys.sleep(1000);
        webSocket.close();
        httpClient.stop();
        httpServer.stop();

That is all! You can send and receive and it is fast. Next we show you the full example and then we add more call backs using Java 8 Lambdas.

Full example using WebSocket client and server

import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.server.HttpServer;
import io.advantageous.qbit.http.websocket.WebSocket;
import org.boon.core.Sys;

import static io.advantageous.qbit.http.client.HttpClientBuilder.httpClientBuilder;
import static io.advantageous.qbit.http.server.HttpServerBuilder.httpServerBuilder;

public class EchoWebSocket {


    public static void main(String... args) {


        /* Create an HTTP server. */
        HttpServer httpServer = httpServerBuilder()
                .setPort(8080).build();

        /* Setup WebSocket Server support. */
        httpServer.setWebSocketOnOpenConsumer(webSocket -> {
            webSocket.setTextMessageConsumer(message -> {
                webSocket.sendText("ECHO " + message);
            });
        });

        /* Start the server. */
        httpServer.start();

        /** CLIENT. */

        /* Setup an httpClient. */
        HttpClient httpClient = httpClientBuilder()
                .setHost("localhost").setPort(8080).build();
        httpClient.start();

        /* Setup the client websocket. */
        WebSocket webSocket = httpClient
                .createWebSocket("/websocket/rocket");

        webSocket.setTextMessageConsumer(message -> {
            System.out.println(message);
        });
        webSocket.openAndWait();

        /* Send some messages. */
        webSocket.sendText("Hi mom");
        webSocket.sendText("Hello World!");


        Sys.sleep(1000);
        webSocket.close();
        httpClient.stop();
        httpServer.stop();
    }

}

A more involved example showing onClose, onOpen, onError, and onMessage

First lets show the server side setup.

Server side Java WebSocket event handling with Java 8 Lambda

        /* Create an HTTP server. */
        HttpServer httpServer = httpServerBuilder()
                .setPort(8080).build();

        /* Setup WebSocket Server support. */
        httpServer.setWebSocketOnOpenConsumer(webSocket -> {

            /** Set up onMessage. */
            webSocket.setTextMessageConsumer(message -> {
                webSocket.sendText("ECHO " + message);
            });

            /** Set up onClose. */
            webSocket.setCloseConsumer(obj -> {
                puts("SERVER CLOSE ");
            });

            /** Set up onError. */
            webSocket.setErrorConsumer(error -> {
                puts("SERVER ERROR", error);
            });

        });
Now here is the client side setup.

Client side Java WebSocket event handling with Java 8 Lambda

        /* Setup an httpClient. */
        HttpClient httpClient = httpClientBuilder()
                .setHost("localhost").setPort(8080).build();
        httpClient.start();

        /* Setup the client websocket. */
        WebSocket webSocket = httpClient
                .createWebSocket("/websocket/rocket");

        webSocket.setTextMessageConsumer(message -> {
            System.out.println("CLIENT ON MESSAGE \n" + message);
        });

        /** Set up onClose. */
        webSocket.setCloseConsumer(obj -> {
            puts("CLIENT CLOSE");
        });


        /** Set up onError. */
        webSocket.setErrorConsumer(error -> {
            puts("CLIENT ERROR", error);
        });
Now the complete example

Complete Java 8 Lambda WebSocket example

package io.advantageous.qbit.http.jetty;

import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.server.HttpServer;
import io.advantageous.qbit.http.websocket.WebSocket;
import org.boon.core.Sys;

import static io.advantageous.qbit.http.client.HttpClientBuilder.httpClientBuilder;
import static io.advantageous.qbit.http.server.HttpServerBuilder.httpServerBuilder;
import static org.boon.Boon.puts;

/**
 * Created by rhightower on 2/16/15.
 */
public class EchoWebSocketMoreComplex {

    public static void main(String... args) {


        /* Create an HTTP server. */
        HttpServer httpServer = httpServerBuilder()
                .setPort(8080).build();

        /* Setup WebSocket Server support. */
        httpServer.setWebSocketOnOpenConsumer(webSocket -> {

            /** Set up onMessage. */
            webSocket.setTextMessageConsumer(message -> {
                webSocket.sendText("ECHO " + message);
            });

            /** Set up onClose. */
            webSocket.setCloseConsumer(obj -> {
                puts("SERVER CLOSE ");
            });

            /** Set up onError. */
            webSocket.setErrorConsumer(error -> {
                puts("SERVER ERROR", error);
            });

        });

        /* Start the server. */
        httpServer.start();

        /** CLIENT. */

        /* Setup an httpClient. */
        HttpClient httpClient = httpClientBuilder()
                .setHost("localhost").setPort(8080).build();
        httpClient.start();

        /* Setup the client websocket. */
        WebSocket webSocket = httpClient
                .createWebSocket("/websocket/rocket");

        webSocket.setTextMessageConsumer(message -> {
            System.out.println("CLIENT ON MESSAGE \n" + message);
        });

        /** Set up onClose. */
        webSocket.setCloseConsumer(obj -> {
            puts("CLIENT CLOSE");
        });


        /** Set up onError. */
        webSocket.setErrorConsumer(error -> {
            puts("CLIENT ERROR", error);
        });

        webSocket.openAndWait();

        /* Send some messages. */
        webSocket.sendText("Hi mom");
        webSocket.sendText("Hello World!");


        Sys.sleep(1000);
        puts("----------- SHUTDOWN --------------");
        webSocket.close();
        Sys.sleep(100);
        httpClient.stop();
        httpServer.stop();
    }

}

Output from last example

CLIENT ON MESSAGE 
ECHO Hi mom
CLIENT ON MESSAGE 
ECHO Hello World!
----------- SHUTDOWN -------------- 
SERVER CLOSE  
CLIENT CLOSE 





Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training