New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

websocket-as-promised

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

websocket-as-promised

Promise-based WebSocket wrapper

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
increased by26.27%
Maintainers
1
Weekly downloads
 
Created
Source

websocket-as-promised

Build Status npm license

Promise-based W3C WebSocket wrapper

This library allows to use Promises when connecting, disconnecting and messaging with WebSocket server.

Installation

npm install websocket-as-promised --save

Usage in browser

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.'));

Usage in Node.js

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.'));
  ...

Messaging

  1. 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'}
    
  2. 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
    

API

WebSocketAsPromised

Kind: global class

new WebSocketAsPromised(url, [options])

Constructor. Unlike original WebSocket it does not immediately open connection. Please call open() method manually to connect.

// todo: use options.js

ParamTypeDefaultDescription
urlStringWebSocket URL
[options]Object
[options.createWebSocket]functionurl => new Websocket(url)custom WebSocket creation function
[options.packMessage]functioncustom packing message function
[options.packMessage]functioncustom unpacking message function
[options.timeout]Number0default timeout for requests

wsp.ws ⇒ WebSocket

Returns original WebSocket instance created by options.createWebSocket.

Kind: instance property of WebSocketAsPromised

wsp.isOpening ⇒ Boolean

Is WebSocket connection in opening state.

Kind: instance property of WebSocketAsPromised

wsp.isOpened ⇒ Boolean

Is WebSocket connection opened.

Kind: instance property of WebSocketAsPromised

wsp.isClosing ⇒ Boolean

Is WebSocket connection in closing state.

Kind: instance property of WebSocketAsPromised

wsp.isClosed ⇒ Boolean

Is WebSocket connection closed.

Kind: instance property of WebSocketAsPromised

wsp.onMessage ⇒ Channel

Event channel triggered every time when message from server arrives. Listener accepts two arguments:

  1. jsonData if JSON parse succeeded
  2. original event.data

Kind: instance property of WebSocketAsPromised
See: https://vitalets.github.io/chnl/#channel
Example

wsp.onMessage.addListener((data, jsonData) => console.log(data, jsonData));

wsp.onClose ⇒ 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

wsp.open() ⇒ Promise

Opens WebSocket connection.

Kind: instance method of WebSocketAsPromised

wsp.request(data, [options]) ⇒ Promise

Performs request and waits for response.

Kind: instance method of WebSocketAsPromised

ParamType
dataObject
[options]Object
[options.requestId]String
[options.requestIdPrefix]String
[options.timeout]Number

wsp.send(data)

Sends any data by WebSocket.

Kind: instance method of WebSocketAsPromised

ParamType
dataString | ArrayBuffer | Blob

wsp.close() ⇒ Promise

Closes WebSocket connection.

Kind: instance method of WebSocketAsPromised

License

MIT @ Vitaliy Potapov

Keywords

FAQs

Package last updated on 15 Sep 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc