1.0.2 • Published 5 years ago

sync-websocket v1.0.2

Weekly downloads
6
License
-
Repository
github
Last release
5 years ago

sync-websocket

A simple way to wait for the result of a websocket request.

When you need to await for a message, use the send method, it will include an id to the request, when you receive a message containing this id, the SyncWebsocket object will resolve the pending Promise.

In case that wasn't clear enough, when the other peer returns a result, its message should include the id.


Install

npm install sync-websocket

Usage

const SyncWebsocket = require('sync-websocket')

const syncws = new SyncWebsocket()

API

Class: SyncWebsocket

new SyncWebsocket

Creates a new synchronous websocket handler.

send(socket, data)

  • socket {WebSocket}
  • data {Object}

Returns a Promise

Sends a message to the defined socket, with the specified data. Adds an id field, that would be overwritten if it already exists.

The send method stringifies the data. So the other peer will receive a string.

receive(data)

  • data {Object}

Resolves the promise identified by the id field found in data