Socket
Socket
Sign inDemoInstall

wspromisify

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wspromisify

Makes it easier to handle different data via WebSockets.


Version published
Weekly downloads
58
decreased by-41.41%
Maintainers
1
Weekly downloads
 
Created
Source

WebsocketPromisify

Makes websocket's API just like REST with Promise-like API, with native Promises.

// If you detected some bug or so, please, fill an issue.

Makes a Promise-like WebSocket connection. Features (almost all are tunable via constructor config below.)

  • Async/await ready.
  • ES-module and commonjs built-in.
  • Types (d.ts) included.
  • Automatically reconnects.
  • Any id and data keys to negotiate with your back-end.
  • Lazy connect: connects only if something sent, then send all of them!
  • Supports middleware. E.g. you can use 'ws' package in Node!
  • Custom easy .on method with or without condition: analog to .addEventListener.
  • Can log messages/frames/response time into console or wherever you want to. (Hello, firefox 57+!)
  • Any protocols field.
  • Rejects if sent into closed socket or after some timeout without response.
  • If something sent before connection is connection is estabilished, it sends when its ready.

Default constructor config is

{
  // You can also use plain text and blobs in future.
  data_type: 'json',
  // Debug features. Not required.
    log: ((event, time, message) => null),
    // Will count milliseconds for responses and put them to log function above.
    timer: false,
  // Set up.
    // Required. URL to connect.
    url: 'localhost',
    // Timeout after sending a message before it dropes with error.
    timeout: 1400,
    // Reconnect timeout in seconds or null.
    reconnect: 2,
    // Lazy connect: connects only if something sent (then sends all of them!)
    lazy: false,
    // You can set your own middleware here.
    adapter: ((host, protocols) => new WebSocket(host, protocols)),
    // WebSocket constructor's protocol field.
    protocols: [],
    // Unique id's and data keys to negotiate with back-end.
    server: {
      id_key: 'id',
      data_key: 'data'
    }
}

Methods:


  // sends any type of message.
  send(message),
  on(event_name, handler, predicate),
  close()

Example:


  import WSP from 'wspromisify'


  const somehost = 'example.com:8080'

  const someFunction = async () => {
    const ws = new WSP({
      url: `${somehost}/ws`,
      timeout: 2e3,
      timer: true,
      log(event, time, message = '') {
        if(time !== null) {
          console.log(event, `in ${time}ms`, message)
        } else {
          console.log(event, message)
        }
      }
    })

    try {
      const data = await ws.send({catSaid: 'Meow!'})
      console.log({data})
    } catch(error) {
      console.error('Cannot send a message due to ', error)
    }
  }

  someFunction()

Keywords

FAQs

Package last updated on 12 Dec 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