Minimal Events/RPC for WebSockets.
Last updated 4 years ago by goto-bus-stop .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ws-events 
SYNC missed versions from official npm registry.


Minimal Events for WebSockets.


ws-events decorates a WebSocket instance. It works both on the server and the client side.

On the server side:

const wsEvents = require('ws-events')
const Server = require('ws').Server

const wss = new Server()
wss.on('connection', (ws) => {
  const events = wsEvents(ws)
  events.emit('hello', {
    any: 'json'

  events.on('world', (arg) => {

On the client side:

const wsEvents = require('ws-events')
const ws = wsEvents(new WebSocket('ws://localhost'))

ws.on('hello', (data) => {
  // data.any === 'json'
  ws.emit('world', 'Hello from a browser \\o')


events = wsEvents(socket)

Create a ws-events emitter. The emitter wraps the passed-in socket, so all native WebSocket methods can still be used.

socket is a standard WebSocket instance.

const socket = wsEvents(new WebSocket(...))
socket.addEventListener('open', () => {
  // Using a ws-events method.
  socket.emit('ya ya')
  // Using a native method.

events.on(eventName, cb): this

Register an event handler., cb): this

Remove an event handler. this

Remove all handlers for the given event. this

Remove all handlers for all events.

events.emit(eventName, ...arguments): this

Emit an event. When emitting on the server, the handlers on the client will fire. When emitting on the client, the handlers on the server will fire.

events.hasListeners(eventName): bool

Check if there are any handlers for an event.

events.listeners(eventName): Array<function>

Return the listeners for an event.



Current Tags

  • 1.0.0                                ...           latest (4 years ago)

1 Versions

  • 1.0.0                                ...           4 years ago
Maintainers (1)
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (0)
Dependents (1)

Copyright 2014 - 2017 © |