@davidyu1983/rx-websocket
TypeScript icon, indicating that this package has built-in type declarations

11.0.6 • Public • Published

RxWebsocket

npm version

pure rxjs implement websocket. support json and protobuf;

ChangeLog

how to install

yarn add @davidyu1983/rx-websocket or npm i @davidyu1983/rx-websocket

how to use

  • app.module.ts
import {RxWebsocketModule} from '@davidyu1983/rx-websocket';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    RxWebsocketModule.forRoot({
      wsEndPoint: environment.wsEndPoint,
    }),
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {
}
  • app.component.ts
import {Component, OnInit} from '@angular/core';
import {RxWebsocketService} from 'rx-websocket';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {

  message$ = this.websocketService.messages$;

  constructor(private websocketService: RxWebsocketService) {
  }

  ngOnInit(): void {
    // if you set 'autoConnect:false' you need below code. 
    // this.websocketService.connect();
  }

}
  • app.component.html
<section>
  <p>{{message$|async}}</p>
</section>

forRoot config

RxWebsocketModule.forRoot({
  wsEndPoint: environment.wsEndPoint,// your ws endpoint; required
  dataType: 'json', //'json'|'protobuf';  default is 'json';
  autoConnect: true, // whether auto  connect when construct; default is ture
  reconnectInterval: 1000 * 5, // reconnect  interval  unit is ms ; default is 5s .
  showLog: true, // whether show log; default is true;
})

RxWebsocketService Api

export declare class RxWebsocketService {
  messages$: Observable<unknown>; //when client received message; message$ will push data;
  sendMessage(msg: any): void; // send message to ws-server;
  connect(): void; // connect ws-service; if the 'auto-connect' set is false;
}

Package Sidebar

Install

npm i @davidyu1983/rx-websocket

Weekly Downloads

1

Version

11.0.6

License

MIT

Unpacked Size

70.3 kB

Total Files

34

Last publish

Collaborators

  • davidyu1983