Spring WebSocket support for Servlet stack applications providing comprehensive infrastructure for real-time bidirectional communication
npx @tessl/cli install tessl/maven-org-springframework--spring-websocket@7.0.0Comprehensive WebSocket infrastructure for real-time bidirectional communication. Supports raw WebSocket, SockJS fallback, and STOMP messaging.
Maven:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>7.0.1</version>
</dependency>Gradle:
implementation 'org.springframework:spring-websocket:7.0.1'// Core WebSocket API
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
// Handler base classes
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.handler.BinaryWebSocketHandler;
// Configuration
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
// STOMP messaging
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;public class MyHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message)
throws Exception {
session.sendMessage(new TextMessage("Echo: " + message.getPayload()));
}
}@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new MyHandler(), "/ws")
.setAllowedOrigins("*");
}
}@Configuration
@EnableWebSocketMessageBroker
public class StompConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/stomp").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic", "/queue");
registry.setApplicationDestinationPrefixes("/app");
}
}@Controller
public class MessageController {
@MessageMapping("/chat")
@SendTo("/topic/messages")
public String handleMessage(String message) {
return "Received: " + message;
}
}public interface WebSocketHandler {
void afterConnectionEstablished(WebSocketSession session) throws Exception;
void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception;
void handleTransportError(WebSocketSession session, Throwable exception) throws Exception;
void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception;
boolean supportsPartialMessages();
}public interface WebSocketSession {
String getId();
URI getUri();
HttpHeaders getHandshakeHeaders();
Map<String, Object> getAttributes();
Principal getPrincipal();
void sendMessage(WebSocketMessage<?> message) throws IOException;
boolean isOpen();
void close(CloseStatus status) throws IOException;
}// Text message
new TextMessage(CharSequence payload);
new TextMessage(CharSequence payload, boolean isLast); // For partial messages
// Binary message
new BinaryMessage(ByteBuffer payload);
new BinaryMessage(byte[] payload);
// Control messages
new PingMessage();
new PongMessage();CloseStatus.NORMAL // 1000
CloseStatus.GOING_AWAY // 1001
CloseStatus.PROTOCOL_ERROR // 1002
CloseStatus.NOT_ACCEPTABLE // 1003
CloseStatus.BAD_DATA // 1007
CloseStatus.POLICY_VIOLATION // 1008
CloseStatus.TOO_BIG_TO_PROCESS // 1009
CloseStatus.SERVER_ERROR // 1011Core WebSocket API - Complete core interfaces and classes
Adapter Support - JSR-356 and Jetty adapters
Handler Support - Base classes, decorators, thread-safe wrappers
Configuration - @EnableWebSocket, @EnableWebSocketMessageBroker
Server Support - Handshake handlers, interceptors, upgrade strategies
Client Support - WebSocketClient, connection managers
STOMP Messaging - STOMP protocol support
SockJS Support - Fallback transports
SockJS Frames - Frame encoding/decoding
SockJS Client - Client-side SockJS implementation