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:8080npm install @storeon/websocket
# or
yarn add @storeon/websocketThis module has two core functionality:
- sending all events to server
- receiving and dispatching events from server
First parameter is address of WebSocket server:
type url = StringSecond 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 | NumberFourth parameter is how often in millisecond module should check if connection is alive (default 2000):
type pingPongInterval = Void | NumberOnly url is not optional parameter.
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.
MIT
This module based on redux-websocket repository.