UNPKG

@pecometer/pecots-websocket-client

Version:

PecoTS Framework Websocket Client

43 lines (33 loc) 1.16 kB
# A websocket client for use with PecoTS Framework websockets Once initialised it will connect and maintain the websocket connection according to the settings sent by the server during the handshake process ## Init ```typescript const ws = new PecoTsWebSocket(); const url = 'ws:...'; const jwt = 'your user auth jwt token'; ws.init(url,jwt); const listener = ws.register('an-event-from-the-server').subscribe((data:any)=>{ ... }); ... ``` ## HTTP over the websocket If enabled on the backend application, you may pass HTTP requests over the websocket connection to the backend. The websocket will return the server response as you would expect over a standard HTTP request. ```typescript // To perform fake http requests over the websocket (and if enabled on the backend application), simply // call the method required with the url and data for the request ws.post('/someurl',{id: 1,some:'data'}).subscribe(data=>{ // handle the response ... }) ... ``` ## Cleaning up Once the listener is no longer required you must call the unsubscribe method on the Observable to avoid memory leaks ```typescript ... listener.unsubscribe(); ... ```