CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-springframework--spring-websocket

Spring WebSocket support for Servlet stack applications providing comprehensive infrastructure for real-time bidirectional communication

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Spring WebSocket

Comprehensive WebSocket infrastructure for real-time bidirectional communication. Supports raw WebSocket, SockJS fallback, and STOMP messaging.

Installation

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 Imports

// 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;

Quick Start

Basic WebSocket Handler

public class MyHandler extends TextWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message)
            throws Exception {
        session.sendMessage(new TextMessage("Echo: " + message.getPayload()));
    }
}

WebSocket Configuration

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new MyHandler(), "/ws")
                .setAllowedOrigins("*");
    }
}

STOMP Configuration

@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");
    }
}

STOMP Message Handler

@Controller
public class MessageController {
    @MessageMapping("/chat")
    @SendTo("/topic/messages")
    public String handleMessage(String message) {
        return "Received: " + message;
    }
}

Architecture Layers

  1. Core - WebSocket API (handlers, sessions, messages)
  2. Handler - Base classes and decorators
  3. Configuration - Annotation-based setup
  4. Server - Handshake processing and protocol upgrade
  5. Client - Connection management
  6. Messaging - STOMP sub-protocol support
  7. SockJS - Fallback transports (polling, streaming, SSE)

Core Capabilities

WebSocketHandler Interface

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();
}

WebSocketSession Interface

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;
}

Message Types

// 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 Constants

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            // 1011

Core 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

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.springframework/spring-websocket@7.0.x
Publish Source
CLI
Badge
tessl/maven-org-springframework--spring-websocket badge