Easy to use, plug and play type of Stomp Websocket for Angular based on (ng2-stompjs).
npm install ngx-socksjsSocksJS module for Angular
Socket.IO inspired wrapper over Stomp Websocket for Angular.
Since this is a wrapper of already existing Angular Stomp library, first install the core one:
1. npm i @stomp/ng2-stompjs
and then the wrapper.
2. npm install ngx-socksjs
``ts
import { SocksJSModule } from 'ngx-socksjs';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
SocksJSModule.forRoot({ url: 'ws://
],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule { }
`
We need to configure SocksJSModule module using the object config of type SocksConfig.
Now we pass the configuration to the static method forRoot of SocksJSModule
The SocksJSModule provides now a configured Socket service that can be injected anywhere inside the AppModule.
`typescript
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { SocksJS } from 'ngx-socksjs';
@Injectable({ providedIn: 'root' })
export class SocketService {
constructor(
private socksJS: SocksJS
) { }
onEvent(): Observable
return this.socksJS.fromEvent
}
send(payload: any): void {
this.socksJS.emit('/send', payload);
}
}
`
Most of the functionalities here you are already familiar with.
The only addition is the fromEvent method, which returns an Observable` that you can subscribe to.
Sends a message to the server.
Works the same as in Socket.IO.
Takes an event name and returns an Observable that you can subscribe to.
MIT