0.3.41 • Published 7 years ago

mcs-lite-connect v0.3.41

Weekly downloads
6
License
MIT
Repository
github
Last release
7 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

7 years ago

0.3.40

7 years ago

0.3.39

7 years ago

0.3.38

7 years ago

0.3.37

7 years ago

0.3.36

7 years ago

0.3.35

7 years ago

0.3.34

7 years ago

0.3.33

7 years ago

0.3.32

7 years ago

0.3.31

7 years ago

0.3.30

7 years ago

0.3.29

7 years ago

0.3.28

7 years ago

0.3.27

7 years ago

0.3.26

7 years ago

0.3.25

7 years ago

0.3.24

7 years ago

0.3.23

7 years ago

0.3.22

7 years ago

0.3.21

7 years ago

0.3.20

7 years ago

0.3.19

7 years ago

0.3.18

7 years ago

0.3.17

7 years ago

0.3.16

7 years ago

0.3.15

7 years ago

0.3.14

7 years ago

0.3.13

7 years ago

0.3.12

7 years ago

0.3.11

8 years ago

0.3.10

8 years ago

0.3.9

8 years ago

0.3.8

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago