{"repo":"vitalets/websocket-as-promised","free":true,"listed":false,"github":"https://github.com/vitalets/websocket-as-promised","clone":"git clone https://github.com/vitalets/websocket-as-promised.git","description":"A Promise-based API for WebSockets","language":"JavaScript","stars":601,"topics":["websocket","websocket-client","websocket-api","promise","javascript","nodejs"],"license":"MIT","category":"networking-infra","readme_excerpt":"websocket-as-promised A [WebSocket] client library with [Promise]-based API for browser and Node.js. Example Contents Requirements Installation Usage Browser Node.js Sending data raw JSON binary request / response API Changelog License Requirements global Promise constructor Installation Usage in browser Or with ES7 async / await: Usage in Node.js As there is no built-in WebSocket client in Node.js, you should use a third-party WebSocket npm package. Usage with websocket Here you should use W3C compatible client - W3CWebSocket: Usage with ws Here it is important to define extractMessageData option as event data are passed directly to onmessage handler, not as event.data by spec: Sending raw data To send raw data use .send() method: To handle raw data from server use .onMessage channel: Sending JSON To send JSON you should define packMessage / unpackMessage options: To send data use .sendPacked() method passing json as parameter: To read unpacked data from received message you can subscribe to onUnpackedMessage channel: Sending binary Example of sending Uint8Array : Sending requests websocket-as-promised provides simple request-response mechanism (JSON RPC). Method .sendRequest() sends message with unique requestId and returns promise. That promise get resolved when response message with the same requestId comes. For reading/setting requestId from/to message there are two functions defined in options attachRequestId / extractRequestId : By default requestId value is auto-gener","default_branch":null,"files":null,"tree":[],"storefront":"/r/vitalets","claimed":false,"request_supported":{"post":"https://gitbuyer.com/r/vitalets/websocket-as-promised/request-supported","requests":0},"note":"indexed from public GitHub; nothing is for sale on this page. Clone it from GitHub. Paid listings live at /search."}