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.1.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.1K
decreased by-56.9%
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();

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

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({
  createWebSocket: url => new W3CWebSocket(url)
});
wsp.open('ws://echo.websocket.org')
  .then(...)

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 .send() method that does not return promise:

    wsp.send({foo: 'bar'}); // returns undefined
    

API

WebSocketAsPromised

Kind: global class

new WebSocketAsPromised([options])

Constructor

ParamTypeDefaultDescription
[options]Object
[options.createWebSocket]functionurl => new Websocket(url)custom WebSocket creation method
[options.idProp]String"id"id property name attached to each message
[options.timeout]Number0default timeout for requests

wsp.ws ⇒ WebSocket

Returns original WebSocket instance created by options.createWebSocket.

Kind: instance property of WebSocketAsPromised

wsp.isConnecting ⇒ Boolean

Is WebSocket in connecting state.

Kind: instance property of WebSocketAsPromised

wsp.isConnected ⇒ Boolean

Is WebSocket connected.

Kind: instance property of WebSocketAsPromised

wsp.isDisconnecting ⇒ Boolean

Is WebSocket in disconnecting state.

Kind: instance property of WebSocketAsPromised

wsp.onMessage ⇒ Channel

OnMessage channel with .addListener / .removeListener methods.

Kind: instance property of WebSocketAsPromised
See: https://github.com/vitalets/chnl

wsp.open(url) ⇒ Promise

Opens WebSocket connection.

Kind: instance method of WebSocketAsPromised

ParamType
urlString

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

Performs JSON request and waits for response.

Kind: instance method of WebSocketAsPromised

ParamType
dataObject
[options]Object
[options.timeout]Number

wsp.send(data)

Performs JSON request and does not wait for response.

Kind: instance method of WebSocketAsPromised

ParamType
dataObject

wsp.close() ⇒ Promise

Closes WebSocket connection.

Kind: instance method of WebSocketAsPromised

License

MIT @ Vitaliy Potapov

Keywords

FAQs

Package last updated on 06 Jul 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