RxJS STOMP client for Javascript and Typescript
npm install @stomp/rx-stomp
An RxJS-friendly STOMP-over-WebSocket client for the Web and Node.js.
- Guides, FAQs, and API docs: https://stomp-js.github.io/
- Samples: https://github.com/stomp-js/samples/
html
`
Notes:
- For bundlers (Vite, Webpack, Rollup), prefer installing via npm and importing from "@stomp/rx-stomp".
- The example above uses JSPM import maps for quick demos without a build step.
$3
`bash
$ npm install @stomp/rx-stomp ws
`
`javascript
import { RxStomp } from "@stomp/rx-stomp";
import { WebSocket } from 'ws';
Object.assign(global, { WebSocket });
const rxStomp = new RxStomp();
rxStomp.configure({
brokerURL: 'ws://localhost:15674/ws',
});
rxStomp.activate();
const subscription = rxStomp
.watch({ destination: "/topic/test-rx" })
.subscribe((message) => console.log(message.body));
rxStomp.publish({
destination: "/topic/test-rx",
body: "First message to RxStomp",
});
setTimeout(async () => {
subscription.unsubscribe();
await rxStomp.deactivate();
}, 3000);
`
Tips:
- Node.js does not provide a global WebSocket by default; the snippet above wires the 'ws' package into global scope for compatibility.
- If you prefer not to patch global, provide a factory:
`javascript
import { RxStomp } from "@stomp/rx-stomp";
import { WebSocket } from "ws";
const rxStomp = new RxStomp();
rxStomp.configure({
webSocketFactory: () => new WebSocket("ws://localhost:15674/ws"),
});
rxStomp.activate();
`
$3
`typescript
import { RxStomp, RxStompConfig } from "@stomp/rx-stomp";
const config: RxStompConfig = {
brokerURL: "ws://localhost:15674/ws",
reconnectDelay: 2000,
heartbeatIncoming: 10000,
heartbeatOutgoing: 10000,
};
const rxStomp = new RxStomp();
rxStomp.configure(config);
rxStomp.activate();
const sub = rxStomp.watch({ destination: "/topic/news" }).subscribe(msg => {
console.log("News:", msg.body);
});
`
Further information
See https://stomp-js.github.io/ for instructions and tutorials.
See samples at: https://github.com/stomp-js/samples/.
API documentation at:
https://stomp-js.github.io/api-docs/latest/classes/RxStomp.html.
Troubleshooting
- Connection retries: tune reconnectDelay, maxReconnectDelay, and reconnectTimeMode.
- Heartbeats: set heartbeatIncoming/heartbeatOutgoing to detect stale connections.
- Binary payloads: use binaryBody and set an appropriate content-type`.