Quickstart
This page shows the basic application API using 4.0.1 dependencies. It also applies to the current 4.0.2-SNAPSHOT source; see Maven for local installation.
Requirements
- Java 17
- Spring Framework 7 / Spring Boot 4
- A WebSocket-capable Spring application
Choose a stack
Use one of these artifacts:
wamp2spring-servletfor Spring MVC / servlet applicationswamp2spring-reactivefor Spring WebFlux / reactive applications
Example Maven dependency for the servlet stack:
<dependency>
<groupId>ch.rasc</groupId>
<artifactId>wamp2spring-servlet</artifactId>
<version>4.0.1</version>
</dependency>
Reactive applications use wamp2spring-reactive instead. In a Boot application, also include spring-boot-starter-websocket for servlet WebSocket support or spring-boot-starter-webflux for the reactive stack, with versions managed by your Boot parent/BOM. The WAMP artifacts alone do not supply an embedded web server.
Enable WAMP
Servlet stack:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import ch.rasc.wamp2spring.servlet.EnableServletWamp;
@SpringBootApplication
@EnableServletWamp
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Reactive stack:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import ch.rasc.wamp2spring.reactive.EnableReactiveWamp;
@SpringBootApplication
@EnableReactiveWamp
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Both annotations register a default WAMP endpoint at /wamp.
Add RPC procedures
Methods annotated with @WampProcedure are exposed through the dealer.
import org.springframework.stereotype.Service;
import ch.rasc.wamp2spring.annotation.WampProcedure;
@Service
public class CalculatorService {
@WampProcedure("calc.add")
public int add(int left, int right) {
return left + right;
}
}
Client call example with AutobahnJS:
const result = await session.call("calc.add", [3, 10]);
// result === 13
If you omit the annotation value, the default procedure name is beanName.methodName.
Add Pub/Sub listeners
Methods annotated with @WampListener receive events through the broker.
import org.springframework.stereotype.Service;
import ch.rasc.wamp2spring.WampPublisher;
import ch.rasc.wamp2spring.annotation.WampListener;
@Service
public class TopicService {
private final WampPublisher wampPublisher;
public TopicService(WampPublisher wampPublisher) {
this.wampPublisher = wampPublisher;
}
@WampListener("app.topic")
public void onMessage(String payload) {
this.wampPublisher.publishToAll("app.server.topic", "echo: " + payload);
}
}
Client example:
await session.subscribe("app.server.topic", args => {
console.log(args[0]);
});
session.publish("app.topic", ["hello"]);
Basic client connection
<script src="autobahn.min.js"></script>
<script>
const connection = new autobahn.Connection({
url: "ws://127.0.0.1:8080/wamp",
realm: "default"
});
connection.onopen = async session => {
await session.subscribe("app.server.topic", args => console.log(args));
session.publish("app.topic", ["hello"]);
};
connection.open();
</script>
Clients still send a realm in the initial HELLO message because the WAMP protocol requires it. wamp2spring does not use it to partition routing or expose it in session metadata. Run these examples after connection.onopen; the standalone examples with await belong inside an async function. Serve the page from the same origin as the application, or configure allowed origins as described in Configuration.
Customize the configuration
For most applications, @EnableServletWamp or @EnableReactiveWamp is enough.
For additional control, implement one of these configurer interfaces:
WampConfigurerfor shared feature toggles and argument resolversWampServletConfigurerfor servlet-specific handler registration and decorationWampReactiveConfigurerfor reactive handler mapping and decoration
Example:
import org.springframework.context.annotation.Configuration;
import ch.rasc.wamp2spring.config.Feature;
import ch.rasc.wamp2spring.config.Features;
import ch.rasc.wamp2spring.servlet.WampServletConfigurer;
@Configuration
public class WampConfiguration implements WampServletConfigurer {
@Override
public void configureFeatures(Features features) {
features.disable(Feature.DEALER);
}
}
If you need full control over endpoint paths, subclass the relevant configuration class instead of using the enable annotation. See Configuration for a complete example.
Next steps
Table of Contents
- Introduction
- Quickstart
- Maven
- Modules
- Features
- Configuration
- RPC
- Publish/Subscribe
- Meta APIs
- Security
- Authentication
- Transports
- Spring Session
- Events