1.0.0 • Published 5 years ago

@customjs/ws v1.0.0

Weekly downloads
35
License
-
Repository
-
Last release
5 years ago

WebSocket Service

import { WsModule } from '@customjs/ws';

This module provides a service to communicate with WebSockets in a reactive way.

The service handles error, close, receiveing and sending messages.

Usage

Javascript

  const WS_HOST = 'ws://echo.websocket.org';

  ...

  wsConnection: OpenConnection<any>;

  constructor(
    private ws: WsService
  ) { }

  ngOnInit() {
    this.wsConnection = this.ws.connect(WS_HOST);
  }

  sendMessage() {
    if (this.wsConnection && this.messageToSend) {
      this.wsConnection.channel.send(this.messageToSend);
      this.messageToSend = undefined;
    }
  }

Html

<div *ngIf="(wsConnection.message | async) as message">
  <h3>Last message</h3>
  {{ message }}
</div>