![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
websocket-as-promised
Advanced tools
Promise-based W3C WebSocket wrapper
This library allows to use Promises when connecting, disconnecting and messaging with WebSocket server.
npm install websocket-as-promised --save
const WebSocketAsPromised = require('websocket-as-promised');
const wsp = new WebSocketAsPromised('ws://echo.websocket.org');
wsp.open()
.then(() => console.log('Opened.'))
.then(() => wsp.request({foo: 'bar'}))
.then(response => console.log('Response message received', response))
.then(() => wsp.close())
.then(() => console.log('Closed.'));
As there is no built-in WebSocket client in Node.js, you should use a third-party module. The most popular W3C compatible solution is websocket:
const W3CWebSocket = require('websocket').w3cwebsocket;
const WebSocketAsPromised = require('websocket-as-promised');
const wsp = new WebSocketAsPromised('ws://echo.websocket.org', {
createWebSocket: url => new W3CWebSocket(url)
});
wsp.open()
.then(() => console.log('Opened.'));
...
if you want to send message and expect server's response - you should use .request()
method that returns promise:
wsp.request({foo: 'bar'}); // returns promise
// actually sends message with unique id: {id: 'xxxxx', foo: 'bar'}
// promise waits response message with the same id: {id: 'xxxxx', response: 'ok'}
if you want to just send data and do not expect any response - use .sendJson() / .send()
methods:
wsp.sendJson({foo: 'bar'}); // does not return promise
Kind: global class
WebSocket
Boolean
Boolean
Boolean
Boolean
Channel
Channel
Promise
Promise
Promise
Constructor. Unlike original WebSocket it does not immediately open connection.
Please call open()
method manually to connect.
Param | Type | Default | Description |
---|---|---|---|
url | String | WebSocket URL | |
[options] | Object | ||
[options.createWebSocket] | function | url => new Websocket(url) | custom WebSocket creation method |
[options.idProp] | String | "id" | id property name attached to each message |
[options.timeout] | Number | 0 | default timeout for requests |
WebSocket
Returns original WebSocket instance created by options.createWebSocket
.
Kind: instance property of WebSocketAsPromised
Boolean
Is WebSocket connection in opening state.
Kind: instance property of WebSocketAsPromised
Boolean
Is WebSocket connection opened.
Kind: instance property of WebSocketAsPromised
Boolean
Is WebSocket connection in closing state.
Kind: instance property of WebSocketAsPromised
Boolean
Is WebSocket connection closed.
Kind: instance property of WebSocketAsPromised
Channel
Event channel triggered every time when message from server arrives. Listener accepts two arguments:
event.data
jsonData
if JSON parse succeededKind: instance property of WebSocketAsPromised
See: https://vitalets.github.io/chnl/#channel
Example
wsp.onMessage.addListener((data, jsonData) => console.log(data, jsonData));
Channel
Event channel triggered when connection closed.
Listener accepts single argument {code, reason}
.
Kind: instance property of WebSocketAsPromised
See: https://vitalets.github.io/chnl/#channel
Promise
Opens WebSocket connection.
Kind: instance method of WebSocketAsPromised
Promise
Performs JSON request and waits for response.
Kind: instance method of WebSocketAsPromised
Param | Type |
---|---|
data | Object |
[options] | Object |
[options.timeout] | Number |
Sends JSON data and does not wait for response.
Kind: instance method of WebSocketAsPromised
Param | Type |
---|---|
data | Object |
Sends any WebSocket compatible data.
Kind: instance method of WebSocketAsPromised
Param | Type |
---|---|
data | String | ArrayBuffer | Blob |
Promise
Closes WebSocket connection.
Kind: instance method of WebSocketAsPromised
MIT @ Vitaliy Potapov
FAQs
A WebSocket client library providing Promise-based API for connecting, disconnecting and messaging with server
The npm package websocket-as-promised receives a total of 14,786 weekly downloads. As such, websocket-as-promised popularity was classified as popular.
We found that websocket-as-promised demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.