Stomp over websocket using observable
npm install webstomp-obsconnect, all subscribers to the same Observable will use the same connection.
connect unsubscribe, the connection is automatically closed.
webstompobs.client. Choosing a good client is maybe the most difficult part:
npm run example will open examples in browser and try to connect to RabbitMQ Web-Stomp default Web Sockets url.
npm install webstomp-obs
html
`
`node bundle
node_modules/webstomp-obs/dist/webstompobs.node.js
`
webstompobs will be a global variable.
$3
`js
var webstompobs = require('webstomp-obs');
`
$3
`
import webstompobs from 'webstomp-obs';
`
API
Jeff Mesnil stomp-websocket documentation is still a must read even if the API evolved a little
$3
#### client(url, [options], protocols? )
Uses global WebSocket object for you to return a webstompobs Client object.
with protocols by default to ['v10.stomp', 'v11.stomp', 'v12.stomp']
##### url
Web Sockets endpoint url
##### options false. See binary section.
* heartbeat: default to {incoming: 10000, outgoing: 10000}. You can provide false to cut it (recommended when the server is a SockJS server) or a definition object.
* debug: default to true. Will log frame using console.log
#### over(createWsConnection, [options])
Takes a function createWsConnection returning a WebSocket alike object instance to return a webstomp Client object.
Allows you to use another WebSocket object than the default one. 2 cases for this:
* you do not want webstomp.client to create a default instance for you.
* you are in an old browser or nodejs and do not have a global WebSocket object that webstomp.client can use.
##### createWsConnection
function returning a WebSocket object instance (connected to the chosen url)
##### optionsWebSocket instance, although optional, protocols is often the second parameter.
$3
A client instance can and should be created through webstompobs.client or webstompobs.over
#### connect
Return an Observable which you can subscribe to multiple subscribers.
Disconnect automatically when the last subscriber unsubscribe.
Each time that a new connection is initiated, the observers will receive a new ConnectedClient
$3
A connectedClient instance returned onNext by Client.connect after subscribing (and a connection has been successfully initiated)
Note : to disconnect the connectedClient you need to unsubscribe to the Client.connect, if no subscribers are remaining, the ws will be effectively disconnected
$3
Send a message to the destination "destination"
Note : the websocket should throw an exception if sending is not possible (disconnected ws for example), and the consumer should catch it if necessary
$3
id: any;
commit: any;
abort: any;
};
If no transaction ID is passed, one will be created automatically
#### commit(transaction: any): void;
It is preferable to commit a transaction by calling commit() directly on the object returned by client.begin():
`js
var tx = connectedClient.begin(txid);
...
tx.commit();
`
$3
It is preferable to abort a transaction by calling abort() directly on the object returned by client.begin():
`js
var tx = connectedClient.begin(txid);
...
tx.abort();
`
$3
It is preferable to acknowledge a message by calling ack() directly on the message handled by a subscription callback:
`js
var source = connectedClient.subscribe('webstomp-tasks-example');
var subscription = source.subscribe(
function (message) {
message.ack();
`
$3
`js
var source = connectedClient.subscribe('webstomp-tasks-example');
var subscription = source.subscribe(
function (message) {
message.nack();
`
$3
Will give back an Observable to subscribe to get the receipt event.
$3
Will give back an Observable to subscribe to get the connection errors.
$3
Will give back an Observable to subscribe to get the error event.
$3
Initialise an UNIQUE subscription with the websocket on a destination (usefull for the queue for example)
To unsubscribe to the destination, you can call subscription.unsubscribe()
`js
var source = connectedClient.subscribe('webstomp-tasks-example');
var subscription = source.subscribe(
function (message) {
onMessage(currentLine, message);
},
function (err) {
onError(err);
},
function () {
console.log('Completed');
}
);
`
$3
Initialise a BROADCAST subscription with the websocket per destination (usefull for the topic for example),
by using this, if you subscribes multiple times to the same destination using the same connected client,
no new subscription will be initiated.
To unsubscribe to the destination, you call call subscription.unsubscribe()
The unsubscribe will be effective when no other is subscribing to the same destination.
`js
var source = connectedClient.subscribeBroadcast('webstomp-tasks-example');
var subscription = source.subscribe(
function (message) {
onMessage(currentLine, message);
},
function (err) {
onError(err);
},
function () {
console.log('Completed');
}
);
`
#### debug
Will use console.log` by default. Override it to update its behavior.