Building reactive applications using websockets
Websockets are a great way to create decoupled communication channels for your applications. Doing it asynchronously is even better and cooler for non-blocking features.
This recipe will show how to do it.
Getting ready
Let's first add our Java EE 8 dependency:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>How to do it...
- The first thing we need is our server endpoint:
@Singleton
@ServerEndpoint(value = "/asyncServer")
public class AsyncServer {
private final List<Session> peers = Collections.synchronizedList(new ArrayList<>());
@OnOpen
public void onOpen(Session peer){
peers.add(peer);
}
@OnClose
public void onClose(Session peer){
peers.remove(peer);
}
@OnError
public void onError(Throwable t)...