Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save antonioaguilar/ee5b26aea38ed4f0d4aaabf5939d52c4 to your computer and use it in GitHub Desktop.
Save antonioaguilar/ee5b26aea38ed4f0d4aaabf5939d52c4 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