WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C. Undertow provides support for Websockets out of the box. Undertow core provides a XNIO based API, which uses the Xnio Channel interface to provide access to the web socket at a low level.
Most users will want a higher level interface than this and Undertow provides a JSR-356 implementation through the undertow-websocket-jsr
. It is quite useful and has awesome performance, but it stills depends on Servlet API. As Servlet containers hurts Kikaha's philosophy due to its complexity, micro Routing API also offers a tiny layer for dealing with Web Socket Resources.
Note: to use WebSocket routing be sure to include the uRouting API dependency on your project.
The bellow sample shows how to listen to messages sent in a chat room.
import kikaha.urouting.api.*;
@WebSocket( "chat/{room-id}" )
public class ChatResource {
@OnMessage
public void onMessage(
@PathParam("room-id") final Long id,
final String message ) {
System.out.println( id + ":" + message );
}
}
@PathParam
annotation@HeaderParam
annotation the same way you use for other ordinary HTTP requests.Similarly to JSR-356, you are able to listen to any web socket events (on error
, on open
and on close
) using annotations.
import io.undertow.websockets.core.CloseMessage;
import kikaha.urouting.api.*;
@WebSocket( "chat/{room-id}" )
public class ChatResource {
@OnOpen
public void onOpen(
@HeaderParam( "SESSION" ) final String sessionId ) {
System.out.println( sessionId );
}
@OnError
public void onError( final Throwable cause ) {
System.err.println( cause.getMessage() );
}
@OnClose
public void onClose( final CloseMessage cm ) {
System.out.println( cm.getReason() );
}
}
Kikaha wrapped the Undertow WebSocket session in a WebSocketSession
instance. This session instance is able to send messages to other peers, or even broadcast message to them. You can inject the a WebSocketSession
instance wherever the event method you need.
import io.undertow.websockets.core.CloseMessage;
import kikaha.core.modules.websocket.WebSocketSession;
import kikaha.urouting.api.*;
@WebSocket( "chat/{room-id}" )
public class ChatResource {
@OnMessage
public void onMessage( WebSocketSession session, String message ) {
session.broadcast( session.userPrincipal() + " just sent: " + message );
}
@OnOpen
public void onOpen( WebSocketSession session ) {
session.broadcast( session.userPrincipal() + " has entered into the chat room" );
}
@OnError
public void onError( final Throwable cause ) {
System.err.println( cause.getMessage() );
}
@OnClose
public void onClose( WebSocketSession session, CloseMessage cm ) {
session.broadcast( session.userPrincipal() + " left the chat room" );
}
}
A brief description of some methods available on WebSocketSession:
WELCOME About Kikaha philosophy
GETTING STARTED Getting started in 1 minute Creating a Kikaha maven project Architecture overview
TUTORIALS Logging configuration Configuring the server Creating your first HTTP route Kikaha's command line interface Configuring your favorite IDE Wro4j Integration
CORE FEATURES HTTP and HTTPS Routing static assets Dependency injection Authentication and authorization Smart routes
ESSENTIAL MODULES μRouting API WebSocket Routing Database Connection Pool JSON with Jackson Protobuf Mustache Templates Rocker Templates BCrypt
CLOUD MODULES Overview of Cloud Modules Consul.io Codahale's Metrics Auth0 Single Sign-On μWorkers - Actor-like API Hazelcast
AWS-RELATED MODULES Overview of AWS-Related Modules Deploying Applications on AWS AWS IAM Credentials AWS EC2 AWS SQS queues AWS CloudWatch metrics AWS Application Load Balancer AWS Lambda functions AWS X-Ray
ADVANCED TOPICS Creating custom modules Routing with Undertow's API Creating custom cloud modules