Websockets + SockJs: ConnectCallback not executing on client side when using stompclient - spring

My issue is:
$scope.socket = new SockJS("ws/ws");
$scope.stompClient = Stomp.over($scope.socket); // Connect to
$scope.stompClient.connect("test", "test", connectCallback, errorCallback);
function connectCallback () {
$scope.stompClient.subscribe('/testurl',someFunc());
}
Here my connectCallback method is not executing when connect is called. Neither errorCallback is executed. It just skips over this line although the socket is opened but I further want to subscribe inside connectCallback definition.
Can u suggest what issue it can be?

I try RabbitMQ with plugin STOMP
https://gist.github.com/tineo/f4e1f977d78d0db438c8
and i use a PHP for create queues, channels, exchanges
https://gist.github.com/tineo/9b25444fa5095f2ef225
Maybe you can reuse my files with Spring.

Related

Resubscribe to Websocket channel in rxjs

Just getting started with rxjs so sorry if the question is basic. I'm trying to implement a websocket client and subscription in nodejs server-side environment with it. I need to connect to a websocket server, subscribe to a channel by sending a specific message, ingest incoming messages. I want the implementation to reconnect and re-subscribe after a disconnect, thus maintaining a persistent open subscription. I've found how to do reconnect and so far the prototype looks as follows:
import WebSocket from 'ws';
import { retry, RetryConfig } from 'rxjs/operators'
import { webSocket } from 'rxjs/webSocket';
const ws$ = webSocket({
url: '...',
WebSocketCtor: WebSocket as any // apparently typings bug
});
// set up reconnects
const retryConfig: RetryConfig = {
delay: 3000,
};
ws$.pipe(retry(retryConfig));
// subscribe to channel by sending a message
ws$.next({cmd: 'subscribe', channel: 'updates'});
ws$.subscribe({
next: (msg) => console.log('msg', msg),
error: (e) => console.error('error', e),
complete: () => console.log('complete')
});
This seems to work so far but after a disconnect it only re-opens the connection and I'm wondering how could I execute custom logic in that case? Specifically here I'd like to re-send the subscription message.
Apart from just an instruction how to do this I'd really appreciate an explanation on why is it so, as rxjs seems to be a bit of a learning curve. Thank you!

How to subscribe to `newBlockHeaders` on local RSK node over websockets?

I'm connecting to RSKj using the following endpoint:
ws://localhost:4444/
... However, I am unable to connect.
Note that the equivalent HTTP endpoint http://localhost:4444/
work for me, so I know that my RSKj node is running properly.
I need to listen for newBlockHeaders, so I prefer to use WebSockets (instead of HTTP).
How can I do this?
RSKj by default uses 4444 as the port for the HTTP transport;
and 4445 as the port for the Websockets transport.
Also note that the websockets endpoint is not at /,
but rather at websocket.
Therefore use ws://localhost:4445/websocket as your endpoint.
If you're using web3.js,
you can create a web3 instance that connects over Websockets
using the following:
const Web3 = require('web3');
const wsEndpoint = 'ws://localhost:4445/websocket';
const wsProvider =
new Web3.providers.WebsocketProvider(wsEndpoint);
const web3 = new Web3(wsProvider);
The second part of your question can be done
using eth_subscribe on newBlockHeaders.
Using the web3 instance from above like so:
// eth_subscribe newBlockHeaders
web3.eth.subscribe('newBlockHeaders', function(error, blockHeader) {
if (!error) {
// TODO something with blockHeader
} else {
// TODO something with error
}
});

How to wait for WebSocket STOMP messages in Cypress.io

In one of my tests I want to wait for WebSocket STOMP messages. Is this possible with Cypress.io?
If the websocket you'd like to access is being established by your application, you could follow this basic process:
Obtain a reference to the WebSocket instance from inside your test.
Attach an event listener to the WebSocket.
Return a Cypress Promise that is resolved when your WebSocket receives the message.
This is a bit difficult for me to test out, absent a working application, but something like this should work:
In your application code:
// assuming you're using stomp-websocket: https://github.com/jmesnil/stomp-websocket
const Stomp = require('stompjs');
// bunch of app code here...
const client = Stomp.client(url);
if (window.Cypress) {
// running inside of a Cypress test, so expose this websocket globally
// so that the tests can access it
window.stompClient = client
}
In your Cypress test code:
cy.window() // yields Window of application under test
.its('stompClient') // will automatically retry until `window.stompClient` exists
.then(stompClient => {
// Cypress will wait for this Promise to resolve before continuing
return new Cypress.Promise(resolve => {
const onReceive = () => {
subscription.unsubscribe() // clean up our subscription
resolve() // resolve so Cypress continues
}
// create a new subscription on the stompClient
const subscription = stompClient.subscribe("/something/you're/waiting/for", onReceive)
})
})

Connect to a sails.js instance via websockets

Is it possible to connect any external application to my sails.js application via WebSockets?
I can use the underlying socket.io embedded in sails.js to talk between the client and server, that's not a problem.
But, I would like to connect another, separate, application to the sails.js server via websockets, and have those two communicate with each other that way, and I am wondering if this is possible?
If so, how can we do this?
Thanks.
Based on SailsJS documentation, we have access to the io object of socket.io, via sails.io.
From that, I just adjusted boostrap.js:
module.exports.bootstrap = function (cb) {
sails.io.on('connection', function (socket) {
socket.on('helloFromClient', function (data) {
console.log('helloFromClient', data);
socket.emit('helloFromServer', {server: 'says hello'});
});
});
cb();
};
Then, in my other nodejs application, which could also be another SailsJS application and I will test that later on, I simply connected and sent a message:
var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000');
socket.emit('helloFromClient', {client: 'says hello'});
socket.on('helloFromServer', function (data) {
console.log('helloFromServer', data);
});
And here are the outputs.
In SailsJS I see:
helloFromClient { client: 'says hello' }
In my client nodejs app I see:
helloFromServer { server: 'says hello' }
So, it seems to be working just fine.

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