npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webstomp-obs

v1.5.0

Published

Stomp over websocket using observable

Downloads

12

Readme

webstomp-obs

This library provides a stomp client for Web browsers and nodejs through Web Sockets with Rxjs Observables.

Project Status

This is a fork of the original [webstomp-client] (https://github.com/JSteunou/webstomp-client) (which is a fork of the original stomp-websocket) re-written to use rxjs Observables. All credits goes to the original authors: Jérôme Steunou & Jeff Mesnil & Jeff Lindsay.

Additional functionalities

Shared websocket connection :

When you connect a client instance via connect, all subscribers to the same Observable will use the same connection.

Automatic reconnection :

As described in options, it is possible to setup an automatic reconnection. When the connection will be available, a new connectedClient will be given back to all the subscribers of that source.

Automatic disconnection :

When the last subscriber to connect unsubscribe, the connection is automatically closed. Note : the connection is automatically restarted on first subscribe

Automatic resubscribe to queue / topic after reconnection :

As we use the observable, it is easy to resubscribe automatically to the queue / topic that were already subscribed to (see tasks example) when the reconnection happens.

Browsers support

Only ES5 compatible modern browsers are supported. If you need a websocket polyfill you can use sockjs

nodejs support

As nodejs does not have a WebSocket object like browsers have, you must choose a websocket client and use webstompobs.over instead of webstompobs.client. Choosing a good client is maybe the most difficult part:

Example

Go into example folder, npm run example will open examples in browser and try to connect to RabbitMQ Web-Stomp default Web Sockets url.

Use

npm install webstomp-obs

Web browser old fashion style

<script type="text/javascript" src="node_modules/webstomp-obs/dist/webstompobs.web.js"></script>
node_modules/webstomp-obs/dist/webstompobs.node.js

webstompobs will be a global variable.

CommonJS

var webstompobs = require('webstomp-obs');

ES6 modules

import webstompobs from 'webstomp-obs';

API

Jeff Mesnil stomp-websocket documentation is still a must read even if the API evolved a little

webstompobs

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
  • maxConnectAttempt: default to 10 - number of reconnecting attempt before throwing an error (calling createWsConnection to attempt the connection) note : 0 won't try to reconnect at all and -1 will infinitely try to reconnect
  • ttlConnectAttempt: default to 1000 - each attempt of reconnection will wait ttlConnectAttempt * before reconnecting
  • binary: default to 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)

options

same as up for client function

VERSIONS

supportedVersions()

List all STOMP specifications supported.

supportedProtocols()

List all websocket STOMP protocols supported. Useful when creating your own WebSocket instance, although optional, protocols is often the second parameter.

Client

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

ConnectedClient

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

send(destination: string, body?: string, headers?: ExtendedHeaders): void;

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

begin(transaction?: any): {

    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():

var tx = connectedClient.begin(txid);
...
tx.commit();

abort(transaction: any): void;

It is preferable to abort a transaction by calling abort() directly on the object returned by client.begin():

var tx = connectedClient.begin(txid);
...
tx.abort();

ack(messageID: any, subscription: any, headers?: {}): void;

It is preferable to acknowledge a message by calling ack() directly on the message handled by a subscription callback:

var source = connectedClient.subscribe('webstomp-tasks-example');

var subscription = source.subscribe(
        function (message) {
            message.ack();

nack(messageID: any, subscription: any, headers?: {}): void;

var source = connectedClient.subscribe('webstomp-tasks-example');

var subscription = source.subscribe(
        function (message) {
            message.nack();

receipt(): Observable;

Will give back an Observable to subscribe to get the receipt event.

connectionError(): Observable;

Will give back an Observable to subscribe to get the connection errors.

error(): Observable;

Will give back an Observable to subscribe to get the error event.

subscribe(destination: string, headers?: SubscribeHeaders): Observable;

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()


var source = connectedClient.subscribe('webstomp-tasks-example');

var subscription = source.subscribe(
        function (message) {
            onMessage(currentLine, message);
        },
        function (err) {
            onError(err);
        },
        function () {
            console.log('Completed');
        }
);

subscribeBroadcast(destination: string, headers?: SubscribeHeaders): Observable;

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.


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.

Binary

It is possible to use binary frame instead of string frame over Web Sockets.

  • client side: set the binary option to true.
  • server side: use a compatible websocket server, like with RabbitMQ Web-Stomp since 3.6

Hearbeat

Not all server are compatible, you may have to deactivate this feature depending the server you are using. For example RabbitMQ Web-Stomp is compatible only since 3.6 with native Web Sockets server.

Authors