2.1.1 • Published 11 months ago

websocket-promise-lite v2.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
11 months ago

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.
2.1.1

11 months ago

2.1.0

11 months ago

2.0.0

11 months ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

5 years ago