loading...

October 21, 2018

wamp2spring – Router for WAMP

wamp2spring is a library used for setting up a WAMP router.

As I am a strong advocate of Spring Boot, I would present you with another way to set up a router for the Web Application Messaging Protocol (WAMP). This is based on the Java library wamp2spring and Spring Boot (you could also use the Spring framework).

Besides setting up one or multiple WAMP clients (for example with Autobahn|Java), a router is needed so that the clients could communicate with each other via WAMP. I have already described the idea in the article Web Application Messaging Protocol.

Even though there are multiple options for settings, it is quite convenient to set up a router with the default settings.

A wamp2spring router itself could register remote procedures, as well as publish on topics, which could be addressed by the wamp clients. This article would present a solution of a router that does not register or publish.

Implement wamp2spring as a Router

As a first step, you have to create a Spring Boot application. This could be done either manually or automatically using your IDE or the Spring Initializr. Next, pick WebSocket as a dependency and add the wamp2spring Web Servlet Stack to your build file (usually pom.xml or gradle.build).

Afterwards, you just have to add the annotation @EnableServletWamp to your main application class and run the server.

@SpringBootApplication
@EnableServletWamp
public class Wamp2springApplication {

    public static void main(String[] args) {
        SpringApplication.run(Wamp2springApplication.class, args);
    }
}

Connect to the wamp2spring Router

wamp2spring does not support multiple realms. This means that every connection, with its procedures and publications, exists in the same realm. It also means that the realm used for connecting to a router is irrelevant.

The default settings create a WebSocket endpoint listening to /wamp. Therefore, the clients should connect to an address that is different from the address used to connect to a Crossbar router. If the router is set up locally, the correct address would be ws://127.0.0.1:8080/wamp.

But with a simple tweak, the default endpoint could be changed. Your class used as @Configuration (in this case the main application class) should extend the class WampServletConfiguration.  This means also that the annotation @EnableServletWamp has to be removed. By overriding the method getWebSocketHandlerPath() the endpoint could be changed.

@SpringBootApplication
public class Wamp2springRouteronlyApplication extends WampServletConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(Wamp2springRouteronlyApplication.class, args);
    }

    @Override
    protected String getWebSocketHandlerPath() {
        return "/endpoint";
    }
}

An example of a WAMP router using wamp2spring could be found on my GitHub page.

Posted in Java, Spring, WAMPTaggs: