npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

websocket-promise-lite

v2.1.1

Published

WebSockets with a promise-based API (async/await). Lite, fast, easy.

Downloads

43

Readme

What is it?

This is an Axios for WebSockets :-)

For what?

To easily work with sockets in async / await style.

Installation

npm i websocket-promise-lite

How to use?

import WebsocketPromiseLiteClient from 'websocket-promise-lite'

const demoFunc = async () => {
	const WS = new WebsocketPromiseLiteClient({
		url: 'wss://ws.postman-echo.com/raw'
	})
	await WS.connectionEstablished()
	const answer = await WS.send({
		myMessage: 'hello websocket!'
	})
	console.log(answer)

	// next actions ...

	WS.close()
}

demoFunc()

Important!

Back-end (websocket server) have to answer with the same "messageId" parameter!

API

methods

  • connectionEstablished: waits for the connection to be established. If fails it reconnects automatically
  • send: sends any unserialized object by WS
  • close: closes WS at normal way (code 1000)
  • destroy: destructor. Removes all event listeners, closes WS connection and clears the list of operations
const WS = new WebsocketPromiseLiteClient(config)

where config is an object with a possible properties:

Config parameters

url: WebSocket URL. Must start with "ws://" or "wss://". The only required param

maxNumberOfReconnects: maximum number of reconnections, after which there will be no more reconnections. If -1 then will be no reconnections. Default Infinity.

pauseBetweenReconnects: pause between reconnections (in ms). If 0 then reconnections will become after randomized pause. Pauses are stored in array [xyz, 3xyz, 5xyz, 8xyz, 12xyz, 17xyz ... 17xyz], where xyz is random number. This is so that all clients reconnects at different time for stable back-end. Default 0.

connectTimeout: time in ms after which connection is considered unsuccessful (and then reconnects again). Default 5000.

serializer: serializer function. For example, CBOR.encode. Default JSON.stringify.

deserializer: deserializer function. For example, CBOR.decode. Default JSON.parse.

binaryType: type of data to be transmitted. In most cases it's "arraybuffer". But You may specify it as blob. Default arraybuffer.

urlAdditionalGenerator: use it when second url part have to be generated by some algorythm. Pass a function.

onConnectionOpen: function (async or not) which is called when a connection is opened successfully for a 1st time

onConnectionClose: function (async or not) which is called when a connection is closed.

onConnectionReOpen: function (async or not) which is called when a connection is before reopen.

onBeforeReOpen: function (async or not) which is called when a connection is before reopen.

onNotPromisedData: function (async or not) which is called when server sends not promised data (simple WS data from back-end)

Tips

  • Don't serialize data before sending via websocket-promise-lite. All the data is serialized by lib before sending.
  • Library adds "messageId" field to object You are sending. Make sure Your data object has no field with the same name.