Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@storeon/websocket

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@storeon/websocket

Module for storeon to sync events through WebSocket

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

Storeon websocket

Storeon logo by Anton Lovchikov

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.

FAQs

Package last updated on 01 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc