Enterprise Java

Web socket Java client for Stomp-Spring server side

Problem :

As an analysis for an issue I had to write a simple java web socket client site code connecting to a Spring based application on the server side with a stomp based web socket broker .

Solution :


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



Sock JS Client

Its the main class , a simple one doing the basic job . As most of the web sockets these days are meant to use SSL , so tried to by pass the trust manager issue by creating a dummy TrustManager .

The input to this code is the topic string you want to subscribe to .

Also dont miss to change the <URL> string in the code before you run , pointing to your web socket url.

package com.client;

import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.SimpleMessageConverter;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SockJsClient {

    public static void main(String[] args) {

        try {
            StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient();

            TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                public void checkServerTrusted(X509Certificate[] certs, String authType) {

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            Map<String, Object> properties = new HashMap<>();
            properties.put("org.apache.tomcat.websocket.SSL_CONTEXT", sc);

            List<Transport> transports = new ArrayList();
            transports.add(new WebSocketTransport(simpleWebSocketClient));

            org.springframework.web.socket.sockjs.client.SockJsClient sockJsClient = new org.springframework.web.socket.sockjs.client.SockJsClient(transports);
            sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());
            WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
            stompClient.setMessageConverter(new MessageConverter() {
                public Object fromMessage(Message<?> message, Class<?> aClass) {
                    return new String((byte[])message.getPayload());

                public Message<?> toMessage(Object o, MessageHeaders messageHeaders) {
                    return null;

            // url : pointing to websocket as sockJs first tries to get info by sending a HTTP request
            // and then sends an upgrade request to ws or wss. So your url should be some thing like htttp://

            URI stompUrlEndpoint = new URI("<url>");

            WebSocketHttpHeaders handshakeHeaders = new WebSocketHttpHeaders();
            StompHeaders connectHeaders = new StompHeaders();

            /*Can set connection header like login , passcode in connectHeaders  */

            stompClient.connect(stompUrlEndpoint.toString(), handshakeHeaders, connectHeaders, new SessionHandler(args[0]), new Object[0]);

        } catch (Exception e) {

Session Handler

Every socket connection need a session handler and here is one which takes care of connection setup and in-coming messages.

package com.client;

import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompFrameHandler;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;

import java.lang.reflect.Type;

public class SessionHandler extends StompSessionHandlerAdapter {

    private final String topicName ;

    public SessionHandler(String topicName) {
        this.topicName = topicName;

    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {

    public void handleException(StompSession session,StompCommand command,StompHeaders headers,byte[] payload,Throwable exception ) {

    public void handleFrame(StompHeaders headers, Object payload) {

    public void handleTransportError(StompSession session, Throwable exception) {

    private void subscribeTo(StompSession session) {
        StompHeaders headers = new StompHeaders();

        session.subscribe(headers, new StompFrameHandler() {
            public Type getPayloadType(StompHeaders stompHeaders) {
                return String.class;

            public void handleFrame(StompHeaders stompHeaders, Object o) {
                System.out.println( " Message is " +
Published on Java Code Geeks with permission by Abhijeet Iyengar, partner at our JCG program. See the original article here: Web socket java client for Stomp-Spring server side.


Opinions expressed by Java Code Geeks contributors are their own.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Abhijeet Iyengar

Abhijeet is a Software Engineer working with financial client . He has been involved in building UI and service based applications.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button