Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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();
// connect
wsp.open('ws://echo.websocket.org')
.then(() => console.log('Connected.'));
// send data and expect response message
wsp.request({foo: 'bar'})
.then(response => console.log('Response message received', response));
// disconnect
wsp.close()
.then(() => console.log('Disconnected.'));
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({
createWebSocket: url => new W3CWebSocket(url)
});
wsp.open('ws://echo.websocket.org')
.then(...)
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 .send()
method that does not return promise:
wsp.send({foo: 'bar'}); // returns undefined
Kind: global class
WebSocket
Boolean
Boolean
Boolean
Channel
Promise
Promise
Promise
Constructor
Param | Type | Default | Description |
---|---|---|---|
[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 in connecting state.
Kind: instance property of WebSocketAsPromised
Boolean
Is WebSocket connected.
Kind: instance property of WebSocketAsPromised
Boolean
Is WebSocket in disconnecting state.
Kind: instance property of WebSocketAsPromised
Channel
OnMessage channel with .addListener
/ .removeListener
methods.
Kind: instance property of WebSocketAsPromised
See: https://github.com/vitalets/chnl
Promise
Opens WebSocket connection.
Kind: instance method of WebSocketAsPromised
Param | Type |
---|---|
url | String |
Promise
Performs JSON request and waits for response.
Kind: instance method of WebSocketAsPromised
Param | Type |
---|---|
data | Object |
[options] | Object |
[options.timeout] | Number |
Performs JSON request and does not wait for response.
Kind: instance method of WebSocketAsPromised
Param | Type |
---|---|
data | Object |
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 1,025 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.