0.3.41 • Published 6 years ago

mcs-lite-connect v0.3.41

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

mcs-lite-connect

Connect MCS with WebSocket.

Installation

$ npm i mcs-lite-connect --save

Usage

connectSocket is a higher-order components based on recompose and W3CWebSocket. It will handle websocket lifecycle for your React component.

import { connectSocket } from 'mcs-lite-connect';

const Component = connectSocket(
  // 1. urlMapper => (ownerProps: Object) => string
  props =>
    'ws://localhost:8000/deviceId/12345/deviceKey/' + props.key,

  // 2. onMessage => (ownerProps: Object) => datapoint => void
  props =>
    datapoint => props.setDatapoint(props.deviceId, datapoint),

  // 3. propsMapper => state => props
  ({ readyState, send, createWebSocket }) => ({
    send,
    isWebSocketClose: readyState.sender === 3,
    reconnect: createWebSocket,
  }),
)(BaseComponent),

API

urlMapper => (ownerProps: Object) => string

Set the URL to be connected. There are two connections:

  • Sender : The Send-Only connection via ${URL}.
  • Viewer : The Read-Only connection via ${URL}/viewer.

onMessage => (ownerProps: Object) => datapoint => void

The callback function of Viewer. It will be invoked when receiving messages (datapoint) from the server.

propsMapper => state => props

A function that maps internal state to a new collection of props that are passed to the base component. There are three states:

  • send(payload: String) : Immediately sends the specified payload (datapoint) to server.
  • readyState: Ready state constants.
  • createWebSocket: A convenience function for reconnecting.

Inspired by

0.3.41

6 years ago

0.3.40

6 years ago

0.3.39

6 years ago

0.3.38

6 years ago

0.3.37

6 years ago

0.3.36

6 years ago

0.3.35

6 years ago

0.3.34

6 years ago

0.3.33

6 years ago

0.3.32

6 years ago

0.3.31

6 years ago

0.3.30

6 years ago

0.3.29

6 years ago

0.3.28

6 years ago

0.3.27

6 years ago

0.3.26

6 years ago

0.3.25

6 years ago

0.3.24

6 years ago

0.3.23

6 years ago

0.3.22

6 years ago

0.3.21

6 years ago

0.3.20

6 years ago

0.3.19

6 years ago

0.3.18

6 years ago

0.3.17

6 years ago

0.3.16

6 years ago

0.3.15

6 years ago

0.3.14

6 years ago

0.3.13

6 years ago

0.3.12

6 years ago

0.3.11

6 years ago

0.3.10

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago