Introduction to STOMP.js
developrx-stomp library.repository·develop·Indexed 21 days ago
https://github.com/stomp-js/stompjsA comprehensive STOMP protocol implementation (v1.0, v1.1, and v1.2) for browser and Node.js environments. It facilitates communication with STOMP-compliant messaging brokers like RabbitMQ and ActiveMQ over WebSockets or TCP. The library supports binary payloads, built-in TypeScript definitions, and provides integration with RxJS via the rx-stomp library.
rx-stomp library.rx-stomp library. It is built on top of STOMP.js and exposes its features as RxJS Observables.@types packages are required; you can use the library in TypeScript projects out-of-the-box.To use STOMP.js directly in a browser without a build step, use an import map to resolve @stomp/stompjs and include es-module-shims to support ESM imports in older browsers.
<script type="importmap">
{
"imports": {
"@stomp/stompjs": "https://ga.jspm.io/npm:@stomp/stompjs@7.0.0/esm6/index.js"
}
}
</script>
<script
async
src="https://ga.jspm.io/npm:es-module-shims@1.5.1/dist/es-module-shims.js"
crossorigin="anonymous"
></script>You can run the test suite in two different environments:
npm run test.npm run karma.Caution: Both NodeJS and browser tests use the same set of test cases and the same queue names. Running both environments simultaneously may cause unexpected test failures due to resource contention on the broker.
# Run tests in NodeJS
$ npm run test
# Run tests in Chrome via Karma
$ npm run karmaTests require a STOMP broker. The project provides a RabbitMQ configuration.
A Dockerfile is provided in the rabbitmq/ directory that includes the necessary plugins and configuration.
docker build -t myrabbitmq rabbitmq/docker run -d -p 15674:15674 myrabbitmqIf using a local RabbitMQ installation:
STOMP and WebStomp plugins.rabbitmq.conf:echo 'web_stomp.ws_frame = binary' >> /etc/rabbitmq/rabbitmq.confspec/config/browser-config.js and spec/config/node-config.js to match your specific broker setup (the defaults are configured for RabbitMQ on localhost).$ docker build -t myrabbitmq rabbitmq/
$ docker run -d -p 15674:15674 myrabbitmqstomp-js, ensure you have node and npm installed. Clone the repository and install the project dependencies using npm i.$ npm iTo use STOMP.js in Node.js, you must install the @stomp/stompjs package along with a WebSocket implementation like ws. Because STOMP.js expects a global WebSocket object, you must manually assign the ws implementation to the global object.
npm install @stomp/stompjs wsThe Stomp class is a legacy factory used to create CompatClient instances. It is deprecated and will be removed in the next major version. Developers should migrate to the Client class using Client#brokerURL or Client#webSocketFactory instead.
There are three primary ways to use this legacy class:
Stomp.client(url, protocols): Creates a client connected to the STOMP server at the specified URL.Stomp.over(ws): An alternative to Stomp.client that allows specifying a custom WebSocket implementation or a factory function. To support auto-reconnection, you should pass a factory function that returns a new socket instance.Stomp.WebSocketClass: A static property used to globally set a non-standard WebSocket class (e.g., for Node.js environments). This approach is also deprecated.// Example: Using the legacy Stomp.client method
var url = "ws://localhost:61614/stomp";
var client = Stomp.client(url);
// Example: Using the legacy Stomp.over method with a factory for reconnection support
var client = Stomp.over(function() {
return new WebSocket('ws://localhost:15674/ws');
});connectionTimeout configuration option allows the client to automatically retry a connection if it is not established within the specified timeframe. Note that in version 6.1.0, this feature is disabled by default to avoid conflicts with certain integrations.Version 7.1.0 introduced two significant features for connection stability and performance:
The client requires a way to establish a WebSocket connection. You can provide this in two ways:
brokerURL: A string representing the WebSocket endpoint (e.g., ws://... or wss://...).webSocketFactory: A function that returns an IStompSocket (e.g., a native WebSocket or SockJS).Precedence: If both are provided, webSocketFactory takes precedence. This is useful for using polyfills like SockJS in environments without native WebSocket support.
// Using brokerURL
client.brokerURL = 'ws://broker.domain.com:15674/ws';
// Using webSocketFactory (e.g., with SockJS)
client.webSocketFactory = function () {
return new SockJS('http://broker.329broker.com/stomp');
};