Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ws-messaging

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ws-messaging

A minimalistic abstraction layer for websockets.

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

ws-messaging

NPM Version Build Status Appveyor status Coverage Status Dependency Status JavaScript Style Guide

Just a really thin abstraction layer on top of WebSocket for Node.js and Web browsers with a Promises and EventEmitter based APIs.

Features

  • Send notifications via an EventEmitter-like API.

  • Request-reply API using promises (works in both directions, without any connection blocking during processing).

  • Built-in auth via WebSocket messages exchange (no more query strings).

  • Reconnection method is provided.

  • Binary messages support via custom encoders/decoders.

  • Reasonable client size (14KB minified, including a Promise polyfill).

Table of Contents

Installation

npm i ws-messaging

Usage

On a server:

const Server = require('ws-messaging')

const port = 8000

function connectionHook (client, authData) {
  // check an authData
  // then assign client events handlers
  // return a promise
}

let server = new Server({port}, {connectionHook})

On a client:

const Client = require('ws-messaging/client')

const url = `ws://localhost:${port}`

const auth = { /* will be authData in connectionHook */ }

let client = new Client(url, {auth})

client.on('someEvent', () => { /* do smth */ })

client.register('someMethod', () => { /* do smth, return a promise */ })

client.on('connection', () => {
  /* now this client can send messages */
  client.send('myEvent', ...someData)
  /* or use request-reply (RPC) API */
  client.invoke('myMethod', ...someArgs)
    .then(result => { /* do smth */ })
    .catch(error => { /* do smth */ })
})

client.on('close', () => {
  /* close, but a client is able to try to reconnect */
  if (!client.terminated) {
    setTimeout(client.reconnect.bind(client), 2000)
  }
})

See tests in test/index.js for more usage examples.

API

Server API and Client API documentation is available online.

Contribute

If you encounter a bug in this package, please submit a bug report to github repo issues.

PRs are also accepted.

License

MIT

Keywords

FAQs

Package last updated on 24 Sep 2016

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