How to get session id on the client side? (WebSocket)

sinedsem picture sinedsem · Jan 18, 2015 · Viewed 15.7k times · Source

Is there any way to do this?

Client side:

function connectWebSocket() {
    var socket = new SockJS('/socket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        console.log("connected");
    });
}

Server side is not important. After the code above has been executed I need to know my session id.

Answer

mariusz2108 picture mariusz2108 · Feb 26, 2016

You can get it from url without making any changes into SockJS library.

var socket = new SockJS('/mqClient');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
        console.log(socket._transport.url); 
        //it contains ws://localhost:8080/mqClient/039/byxby3jv/websocket
        //sessionId is byxby3jv
    });