Giter Site home page Giter Site logo

java-stomp-ws's Introduction

STOMP over WebSocket implementation in Java

This is a STOMP protocol implementation.

Example backend using Spring Boot

The following example is taken from Spring's "Using WebSocket to build an interactive web application" guide but with SockJS disabled.

Configuration

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket");
    }
}

Controller

@Controller
public class GreetingController {

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        return new Greeting("Hello, " + message.getName() + "!");
    }
}

Usage

See the included client example module.

final StompClient stompSocket = new StompClient(URI.create("ws://localhost:8080/gs-guide-websocket"));

// Wait for a connection to establish
boolean connected;
try {
    connected = stompSocket.connectBlocking();
} catch (InterruptedException e) {
    e.printStackTrace();
    return;
}

if (!connected) {
    System.out.println("Failed to connect to the socket");
    return;
}

// Subscribing to a topic
stompSocket.subscribe("/topic/greetings", new StompMessageListener() {

    @Override
    public void onMessage(String message) {
        System.out.println("Server message: " + message);

        // Disconnect
        stompSocket.close();
    }
});

// Sending JSON message to a server
String message = "{\"name\": \"Jack\"}";
stompSocket.send("/app/hello", message);

java-stomp-ws's People

Contributors

gim- avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.