1.2.0 • Published 4 years ago

@storeon/websocket v1.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

Storeon websocket

Tiny module for Storeon which is adding functionality to work with WebSocket. This means that now you can send and receive events by WebSocket.

It is just 401 bytes module (it uses Size Limit to control the size) without any dependencies.

import { websocket } from '@storeon/websocket'

const url = 'ws://localhost:8080'

const store = createStoreon([
  /* all your modules */
  websocket(url)
 ])

// now all dispatched events will be send to server with address ws://localhost:8080

Example of using websocket events functionality

Installation

npm install @storeon/websocket
# or
yarn add @storeon/websocket

Usage

This module has two core functionality:

  • sending all events to server
  • receiving and dispatching events from server

websocket(url, include, reconnectInterval, pingPongInterval)

First parameter is address of WebSocket server:

type url = String

Second parameter is white list of events. All events added to this array will be dispatched/sent.

type include = Void | Array<String>

If no pass the include then all events will be dispatched/sent.

Third parameter is after how many millisecond try reconnect again if connection is lost (default 500):

type reconnectInterval = Void | Number

Fourth parameter is how often in millisecond module should check if connection is alive (default 2000):

type pingPongInterval = Void | Number

Only url is not optional parameter.

Server

The module is sending event like a string. So before sending it stringify all events.

After receiving message from server the module trying to parse this string to json with JSON.parse. If received message is valid json then module dispatching it in storeon.

Each 2000 ms module send ping message to server and if server in 2000 ms doesn’t send back message pong then module trying to reconnect to server.

If server close connection or send error then module trying to reconnect to server.

Example implementation of echo server please see in example.

LICENSE

MIT

Acknowledgments

This module based on redux-websocket repository.

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago