|
| 1 | +--- |
| 2 | +layout: single |
| 3 | +title: 'ng2-stompjs to rx-stomp' |
| 4 | +date: 2022-03-02 00:51:00 +0530 |
| 5 | +categories: guide rx-stomp ng2-stompjs |
| 6 | +toc: true |
| 7 | +--- |
| 8 | + |
| 9 | +These steps have been tested with Angular 7 and Angular 13. So these should work for Angular 7+. |
| 10 | + |
| 11 | +See also [Using rx-stomp with Angular]. |
| 12 | + |
| 13 | +## Instructions |
| 14 | + |
| 15 | +### Compile target |
| 16 | + |
| 17 | +[rx-stomp] uses ES6 classes, so the Angular project needs to set compile target at least `es6`. Please check and adjust `target` in your `tsconfig.json`. |
| 18 | + |
| 19 | +### Uninstall ng2-stompjs, install rx-stomp |
| 20 | + |
| 21 | +```bash |
| 22 | +$ npm uninstall @stomp/ng2-stompjs |
| 23 | +$ npm i @stomp/rx-stomp |
| 24 | +``` |
| 25 | + |
| 26 | +### Change classes and imports |
| 27 | + |
| 28 | +Change [InjectableRxStompConfig] from `@stomp/ng2-stompjs` -> [RxStompConfig] from `@stomp/rx-stomp`. |
| 29 | + |
| 30 | +Next, we will create `RxStompService` and `rxStompServiceFactory`. These were provided by `ng2-stompjs`. If you were using code similar to the tutorials, the following would work as-is. |
| 31 | + |
| 32 | +Create `rx-stomp.service.ts` file inside `src/app/` with the following content: |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { Injectable } from '@angular/core'; |
| 36 | +import { RxStomp } from '@stomp/rx-stomp'; |
| 37 | + |
| 38 | +@Injectable({ |
| 39 | + providedIn: 'root', |
| 40 | +}) |
| 41 | +export class RxStompService extends RxStomp {} |
| 42 | +``` |
| 43 | + |
| 44 | +Create `rx-stomp-service-factory.ts` file inside `src/app/` with the following content: |
| 45 | + |
| 46 | +```typescript |
| 47 | +import { RxStompService } from './rx-stomp.service'; |
| 48 | +import { myRxStompConfig } from './my-rx-stomp.config'; |
| 49 | + |
| 50 | +export function rxStompServiceFactory() { |
| 51 | + const rxStomp = new RxStompService(); |
| 52 | + rxStomp.configure(myRxStompConfig); |
| 53 | + rxStomp.activate(); |
| 54 | + return rxStomp; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +If you are using a custom setup, you may need to adjust the factory code. |
| 59 | + |
| 60 | +### Angular DI setup |
| 61 | + |
| 62 | +You will need to update the configuration for Angular DI. |
| 63 | + |
| 64 | +Remove the provider configuration for `InjectableRxStompConfig` and adjust the setup for `RxStompService`. |
| 65 | + |
| 66 | +```typescript |
| 67 | +providers: [ |
| 68 | + { |
| 69 | + provide: RxStompService, |
| 70 | + useFactory: rxStompServiceFactory, |
| 71 | + }, |
| 72 | +] |
| 73 | +``` |
| 74 | + |
| 75 | +Remove the old imports and add the imports from the newly created modules. |
| 76 | + |
| 77 | +```typescript |
| 78 | +import { RxStompService } from './rx-stomp.service'; |
| 79 | +import { rxStompServiceFactory } from './rx-stomp-service-factory'; |
| 80 | +``` |
| 81 | + |
| 82 | +### Adjust imports |
| 83 | + |
| 84 | +Change import path for `RxStompService` from `@stomp/ng2-stompjs` to the local module. |
| 85 | + |
| 86 | +[rx-stomp]: /api-docs/latest/classes/RxStomp.html |
| 87 | +[InjectableRxStompConfig]: /api-docs/latest/classes/InjectableRxStompConfig.html |
| 88 | +[RxStompConfig]: /api-docs/latest/classes/RxStompConfig.html |
| 89 | +[Using rx-stomp with Angular]: {% link _posts/2022-03-02-rx-stomp-with-angular.md %} |
0 commit comments