|
| 1 | +--- |
| 2 | +layout: single |
| 3 | +title: 'Connection status with @stomp/rx-stomp' |
| 4 | +date: 2025-09-19 19:22:37 +0530 |
| 5 | +categories: guide rx-stomp |
| 6 | +toc: true |
| 7 | +redirect_from: |
| 8 | + - /guide/ng2-stompjs/rx-stomp/2018/12/17/connection-status-ng2-stompjs.html |
| 9 | + - /guide/ng2-stompjs/rx-stomp/2018/12/18/connection-status-ng2-stompjs.html |
| 10 | + - /guide/ng2-stompjs/rx-stomp/2018/12/19/connection-status-ng2-stompjs.html |
| 11 | + - /guide/ng2-stompjs/rx-stomp/connection-status-ng2-stompjs.html |
| 12 | + - /guide/rx-stomp/connection-status-ng2-stompjs.html |
| 13 | +--- |
| 14 | + |
| 15 | +This guide shows how to observe and display the STOMP connection status in an Angular app using @stomp/rx-stomp and RxStomp. |
| 16 | + |
| 17 | +See [rx-stomp with Angular] for a complete Angular setup. |
| 18 | + |
| 19 | +## The API |
| 20 | + |
| 21 | +RxStomp exposes connectionState$ as an RxJS BehaviorSubject. It emits the current state immediately, then on every change. States are defined by the RxStompState enum and correspond to WebSocket readyState semantics: |
| 22 | + |
| 23 | +- CONNECTING |
| 24 | +- OPEN |
| 25 | +- CLOSING |
| 26 | +- CLOSED |
| 27 | + |
| 28 | +Example: log state transitions |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { RxStomp, RxStompState } from '@stomp/rx-stomp'; |
| 32 | + |
| 33 | +const rxStomp = new RxStomp(); |
| 34 | +// ...configure and activate... |
| 35 | +rxStomp.connectionState$.subscribe(state => { |
| 36 | + console.log(RxStompState[state]); // e.g., 'OPEN' |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +In Angular, when using an injected RxStompService (a simple subclass of RxStomp wired via DI), you can map the enum to strings for binding: |
| 41 | + |
| 42 | +```typescript |
| 43 | +import { Observable, map } from 'rxjs'; |
| 44 | +import { RxStompService, RxStompState } from '@stomp/rx-stomp'; |
| 45 | + |
| 46 | +export class StatusComponent { |
| 47 | + connectionStatus$: Observable<string>; |
| 48 | + |
| 49 | + constructor(rxStompService: RxStompService) { |
| 50 | + this.connectionStatus$ = rxStompService.connectionState$.pipe( |
| 51 | + map(state => RxStompState[state]) |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Template (AsyncPipe binds to the latest value): |
| 58 | + |
| 59 | +```angular2html |
| 60 | +<div id="status"> |
| 61 | + <div id="indicator" class="{{ connectionStatus$ | async }}"></div> |
| 62 | + <span id="status-label">{{ connectionStatus$ | async }}</span> |
| 63 | +</div> |
| 64 | +``` |
| 65 | + |
| 66 | +Minimal CSS for a traffic-light indicator: |
| 67 | + |
| 68 | +```css |
| 69 | +div#indicator { |
| 70 | + width: 12px; |
| 71 | + height: 12px; |
| 72 | + border-radius: 6px; |
| 73 | + margin: 0 10px; |
| 74 | + display: inline-block; |
| 75 | + background: #ffbf00; /* CONNECTING, CLOSING */ |
| 76 | +} |
| 77 | + |
| 78 | +div.CLOSED#indicator { background: red; } |
| 79 | +div.OPEN#indicator { background: green; } |
| 80 | +``` |
| 81 | + |
| 82 | +## Responding to successful connection |
| 83 | + |
| 84 | +RxStomp also provides connected$ (Observable). It emits immediately if already connected, and on every subsequent connect. Use it to trigger logic that must run after the session is established (and on reconnects). |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { take } from 'rxjs/operators'; |
| 88 | +import { RxStompService } from '@stomp/rx-stomp'; |
| 89 | + |
| 90 | +// On every (re)connection |
| 91 | +rxStompService.connected$.subscribe(() => { |
| 92 | + console.log('Connected (or reconnected).'); |
| 93 | +}); |
| 94 | + |
| 95 | +// Only once (immediately if already connected) |
| 96 | +rxStompService.connected$.pipe(take(1)).subscribe(() => { |
| 97 | + console.log('Connected once.'); |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +Internally, connected$ is a filtered view of connectionState$ for RxStompState.OPEN: |
| 102 | + |
| 103 | +```typescript |
| 104 | +this.connected$ = this.connectionState$.pipe( |
| 105 | + filter(state => state === RxStompState.OPEN) |
| 106 | +); |
| 107 | +``` |
| 108 | + |
| 109 | +You can similarly watch for CLOSED, CONNECTING, or CLOSING if you need custom handling. |
| 110 | + |
| 111 | +## Notes |
| 112 | + |
| 113 | +- You do not need to delay subscribe/watch calls until after connection; RxStomp queues and (re)subscribes automatically when the broker is connected again. |
| 114 | +- For a complete Angular setup, see [rx-stomp with Angular]. |
| 115 | + |
| 116 | +[RxStomp]: /api-docs/latest/classes/RxStomp.html |
| 117 | +[RxStompConfig]: /api-docs/latest/classes/RxStompConfig.html |
| 118 | +[RxStompState]: /api-docs/latest/miscellaneous/enumerations.html#RxStompState |
| 119 | +[RxStompService]: /api-docs/latest/injectables/RxStompService.html |
| 120 | +[InjectableRxStompConfig]: /api-docs/latest/injectables/InjectableRxStompConfig.html |
| 121 | +[WebSocket States]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState |
| 122 | +[RxJS BehaviorSubject]: http://reactivex.io/rxjs/manual/overview.html#behaviorsubject |
| 123 | +[RxJS Observable]: http://reactivex.io/rxjs/manual/overview.html#observable |
| 124 | +[rx-stomp with Angular]: {% link _posts/2022-03-02-rx-stomp-with-angular.md %} |
0 commit comments