Skip to content

Instantly share code, notes, and snippets.

@bleathem
Last active August 22, 2018 20:54
Show Gist options
  • Save bleathem/50b4dd2fd4377503eaad to your computer and use it in GitHub Desktop.
Save bleathem/50b4dd2fd4377503eaad to your computer and use it in GitHub Desktop.
Creating an Rx.js Observable from a STOMP over Websocket source (with error handling)
// see: https://github.com/jmesnil/stomp-websocket
var client = Stomp.client('ws://...');
client.debug = undefined;
var live = Rx.Observable.create(function (observer) {
console.log('Connecting...')
client.connect(username, password, function(frame) {
console.log(frame.toString());
observer.onNext(frame);
}, function(error) {
observer.onError(new Error(error));
});
})
.flatMap(function() {
return Rx.Observable.create(function (observer) {
client.subscribe('/queue/some_queue', function(message) {
observer.onNext(event);
return function() {
client.disconnect(function() {
console.log('Disconnected.');
});
};
}, function(error) {
observer.onError(new Error(error));
})
})
})
.map(function(message) {
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment