@pecometer/pecots-websocket-client
Version:
PecoTS Framework Websocket Client
43 lines (33 loc) • 1.16 kB
Markdown
Once initialised it will connect and maintain the websocket connection
according to the settings sent by the server during the handshake process
```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)=>{
...
});
...
```
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
...
})
...
```
Once the listener is no longer required you must call the unsubscribe method on the Observable to avoid memory leaks
```typescript
...
listener.unsubscribe();
...
```