Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

webstomp-client

JSteunou86.2kApache-2.01.2.6TypeScript support: included

Stomp client over websocket for browsers and nodejs

stomp, webstomp, websocket

readme

webstomp-client

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

Project Status

This is a fork of the original stomp-websocket re-written in ES6 and incorporate pending pull requests. All credits goes to the original authors: Jeff Mesnil & Jeff Lindsay.

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 webstomp.over instead of webstomp.client. Choosing a good client is maybe the most difficult part:

Example

npm run example will open examples in browser and try to connect to RabbitMQ Web-Stomp default Web Sockets url. node run example/broadcast-node.js will run a dead simple nodejs example.

Use

npm install webstomp-client

Web browser old fashion style

<script type="text/javascript" src="node_modules/webstomp-client/dist/webstomp.min.js"></script>

webstomp will be a global variable.

CommonJS

var webstomp = require('webstomp-client');

ES6 modules

import webstomp from 'webstomp-client';

By default it will load dist/webstomp.js, but the npm package.json es6 entry point to the es6 src file if you prefer loading this version.

API

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

webstomp

client(url, [options])

Uses global WebSocket object for you to return a webstomp Client object.

url<String>

Web Sockets endpoint url

options<Object>
  • protocols: default to ['v10.stomp', 'v11.stomp', 'v12.stomp']
  • 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(ws, [options])

Takes 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.
ws<WebSocket>

WebSocket object instance

options<Object>
  • 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

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 webstomp.client or webstomp.over

connect

  • connect(headers, connectCallback)
  • connect(headers, connectCallback, errorCallback)
  • connect(login, passcode, connectCallback)
  • connect(login, passcode, connectCallback, errorCallback)
  • connect(login, passcode, connectCallback, errorCallback, host)

disconnect(disconnectCallback, headers={})

send(destination, body='', headers={})

subscribe(destination, callback, headers={})

unsubscribe(id, header={})

It is preferable to unsubscribe from a subscription by calling unsubscribe() directly on the object returned by client.subscribe()

var subscription = client.subscribe(destination, onmessage);
...
subscription.unsubscribe(headers);

headers are optionals

onreceive(frame)

If defined on the client instance this function will be called whenever a message is received and in the absence of an explicit subscribe(). Some brokers (at least RabbitMQ) will setup an internal routing topology for RPC patterns when a message is sent with certain headers.

In RabbitMQ it's called Direct Reply-To

On the client

let onreceive(frame)=>{
        console.log('Message received',frame)
}

client.onreceive=onreceive

let headers = {
        'reply-to'  :'/temp-queue/webstomp',
}

client.send('/topic/public.echo.hi.mom','a message')

On the server (using Amqplib for example)

ch.publish('',raw_message.properties.replyTo,Buffer.from('a reply'))

begin([transaction])

If no transaction ID is passed, one will be created automatically

commit(transaction)

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

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

abort(transaction)

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

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

ack(messageID, subscription, headers={})

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

client.subscribe(destination, (message) => {
        // process the message
        // acknowledge it
        message.ack();
    }, {'ack': 'client'}
);

nack(messageID, subscription, headers={})

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

client.subscribe(destination, (message) => {
        // process the message
        // acknowledge it
        message.nack();
    }, {'ack': 'client'}
);

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

Heartbeat

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

changelog

1.2.6 - 26 Oct. 2018

  • Added missing type for Client onReceipt #78

1.2.5 - 10 Oct. 2018

  • Use client.debug instead of console.log for a better control of deprecation warning #75
  • Fallback on 1st STOMP protocol provide at Client creation when ws does not provide a protocol (too many edge cases, let responsability of wrong compatibility on user hands) #74 #75
  • 2nd fallback after warning on 1.2 STOMP protocol #74 #75

1.2.4 - 13 Jul. 2018

  • Fix some ts types for default export of webstomp

1.2.3 - 13 Jul. 2018

  • Move from webpack to rollup & remove mixed export (named & default) to fix #73

1.2.2 - 03 Jul. 2018

  • Fix STOMP CONNECT frame 'accept-version' to match websocket accepted protocol #66

1.2.1 - 03 Jul. 2018

  • Fix: possible race condition with client#onopen callback
  • Some doc

1.2.0 - 10 Oct. 2017

  • Change: Add data object as 2nd parameter for debug #55

1.1.0 - 09 Oct. 2017

  • Change: Default option protocol should not be in options object by default #57
  • Fix: index.d.ts updates
    • Update Frame typings #54
    • These parameters have defaults so they are optional (#53)
    • Connect returns Frame or CloseEvent on error (#51)
  • Fix: Copy headers instead of updating it #43
  • Fix: Stop using a counter and create a near unique id to avoid collision when using several Client instance 2aedaaa

1.0.8 - 18 Sep. 2017

  • Fix TS definition: #49 Fix strictNullChecks incompatibility

1.0.7 - 07 Sep. 2017

  • Fix TS definition: added custom headers to .connect()

1.0.6 - 15 Feb. 2016

  • Fix missing TS definitions file in package.json

1.0.5 - 05 Feb. 2016

  • Add TS definitions file
  • Update all dependencies, especially webpack to 2.2

1.0.4 - 04 Aug. 2016

  • Fixes default options value when an options object is already given with missing values

1.0.3 - 31 July 2016

  • Add check to see if user has already included heart beat information in headers before adding default values (thanks @NathanGloyn)
  • Replace jshint & jscs by eslint (thanks @yeyu456)
  • Add dist folder to github so users without npm and ES6 support can fetch the lib

1.0.2 - 09 Apr. 2016

  • Add an example using nodejs
  • Update the documentation around nodejs and webstomp.over

1.0.1 - 06 Apr. 2016

  • Fixes #2: better ES6 default options for Client
  • Add an example using sockjs & webstomp.over

1.0.0 - 01 Apr. 2016

Initial fork from stomp.js

Breaking changes

  • webstomp.client second parameter is now an object containing protocols, binary, heartbeat. All are optionals.
  • same for webstomp.over
  • client.send parameters are now destination, body, headers instead of destination, headers, body. Easier to set headers optional.
  • onclose error callback now receive the original event

Improvement

  • stomp 1.2 support
  • binary support
  • better unicode utf-8 support
  • UMD compatible
  • client.unsubscribe() can now take a second parameter header to be able to send specific header like persistent: true
  • as a consequence subscription.unsubscribe can now take a header parameter.