how to store websocket connection on client side to use it in new tab? I m using spring websockets - stomp

I need to store websocket connection to use it in new tab on client side, so on opening multiple tab in same browser ,get same connection always, how can i do it?
using this funtion for socket connection:
function connect() {
var socket = new SockJS('/spring-mvc-java/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
stompClient.subscribe('/topic/messages', function(messageOutput) {
showMessageOutput(JSON.parse(messageOutput.body));
});
});
}
n it makes new connection when i open new tab? any solution

Related

socket.io connection event never fire

server
var io = require('socket.io'),
UUID = require('node-uuid'),
gameport = 3000;
var db = {
waiting_clients: []
};
var logic = {
};
var sio = io.listen(gameport);
sio.sockets.on('connection', function(socket){
var client = {
id: UUID()
};
socket.emit('news', client);
console.log(client.id);
db.waiting_clients.push(client);
});
test client:
var net = require('net');
var client = net.connect({port: 3000},
function(e) { //'connect' listener
console.log('client connected');
client.end();
});
in test client console, it show "client connected". But there are no output in server console
You must use a socket.io client to connect to a socket.io server. Your code shows that you are trying to make a generic TCP connection to a socket.io server. That will not work. The lowest level connection will be established, but then the initial protocol handshake will fail and the socket.io server will drop the connection and you will never get the connection event.
Socket.io has its own connection scheme built on top of webSocket which is built on top of HTTP which is built on top of TCP.
So, to connect to a socket.io server, you must use a socket.io client that runs both the socket.io and webSocket protocol, not a plain TCP socket.

STOMPJS over and subscribe methods not working with Spring, SockJs, npm, webpack

I want to use websocket with SockJs and Stomp client-side, and Java server-side, in a Spring-boot web application. I have installed with npm sockjs-client and stompjs. My client-side code is:
var SockJS = require('sockjs-client');
var Stomp = require("stompjs");
var socket = new SockJS('/webSocketEndPoint');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
stompClient.subscribe('/topic/notification', function (notificationBody) {
alert(JSON.parse(notificationBody.body).content);
});
})
The methods over and subscribe are not working, so the websocket i want to build is not working. IntelliJ say me "Unresolved function or method over" and "Unresolved function or method subscribe ". I've included in my package.json stompjs and sockjs-client (correct version). The initial requires not makes me problem, but i'm not able to run this script..Someone can help me?! Thanks a lot
Maybe because of wrong quotes in require("stompjs")?
Here is my workin example:
let SockJS = require('sockjs-client');
const Stomp = require('stompjs');
function register(registrations) {
let socket = SockJS('/webSocketEndPoint');
let stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
registrations.forEach(function (registration) {
stompClient.subscribe('/topic/notification', function (notificationBody) {
alert(JSON.parse(notificationBody.body).content);
});
});
});
}

Websocket client disconnects automatically after some time

I am using websockets in my laravel project. I have opened a port by PHP that is 8000 and it is working fine. I am connecting to the port by following in in view file:
function WebSocketInit()
{
if ("WebSocket" in window)
{
// Let us open a web socket
ws = new ReconnectingWebSocket("ws://xxx.xxx.xxx.xxx:8000");
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
WebSocketInit();
ws.onopen = function(){
console.log( "Connected." );
};
ws.onclose = function(){
ws.refresh();
console.log( "Disconnected." );
};
It connects well first time but if I refresh my page then it disconnects and do not re-connect most of the time. Sometimes, it connects but takes few seconds. Any help will be appreciated.
Thanks in advance!

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

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.
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
});
The SockJS constructor has an option parameter and there you can pass a custom session id generator as a function:
let sessionId = utils.random_string(8);
let socket = new SockJS('/socket', [], {
sessionId: () => {
return sessionId
}
});
To get session id we need to make some changes into SockJS library.
The string
var connid = utils.random_string(8);
is used to get our id. So, we need only complete it like this:
var connid = utils.random_string(8);
that.sessionId = connid;
and then we can read this field from the client code:
function connectWebSocket() {
var socket = new SockJS('/socket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log("connected, session id: " + socket.sessionId);
});
}
And if we need to know session id before calling connect method we can modify SockJS' constructor and connect method to use client-passed value.

How to tell when a Stomp server disconnected from the Stomp.JS client

I am sending a stomp message over a Sock.JS client. When I disconnect the server I would like a warning message to show up on the client. To do this I have implemented a server side heartbeat
stompClient = Stomp.over(socket);
stompClient.heartbeat.outgoing = 20000;
stompClient.heartbeat.incoming = 20000;
stompClient.connect({}, function(frame) {
...
}
In the Chrome developer console I see the message
POST http://localhost:8080/hello/800/8n_btbxb/xhr_streaming net::ERR_CONNECTION_RESET sockjs-0.3.min.js:27
Whoops! Lost connection to undefined
How can I capture this error message?
As pointed out by muttonUp stomp.js from https://github.com/jmesnil/stomp-websocket/ will overwrite the onclose handler. On the other hand it provides the option to pass an error-callback on connect:
stompClient.connect({}, function(frame) {
...
}, function(message) {
// check message for disconnect
});
Since you will get several kinds of errors delivered to your callback, you have to check the message it it was indeed the "Whoops! [...]" which indicates a connection loss.
Oops just figured it out I will delete in a bit if it doesn't help others. I needed to use SockJS client instead of the Stomp one...
var socket = new SockJS('/hello');
...
socket.onclose = function() {
console.log('close');
stompClient.disconnect();
setConnected();
};

Resources