react-websockets

1.0.0 • Public • Published

react-websocket contributions welcome

react-websocket is a simple to use component for ReactJS applications to setup websocket communication.

Note: This component no longer active development. After +1 year my opinion handling websockets via component is not efficient way.

Consider to use this one https://github.com/lukeed/sockette

Installing

npm install --save react-websocket

Usage

  import React from 'react';
  import Websocket from 'react-websocket';
 
  class ProductDetail extends React.Component {
 
    constructor(props) {
      super(props);
      this.state = {
        count: 90
      };
    }
 
    handleData(data) {
      let result = JSON.parse(data);
      this.setState({count: this.state.count + result.movement});
    }
 
    render() {
      return (
        <div>
          Count: <strong>{this.state.count}</strong>
 
          <Websocket url='ws://localhost:8888/live/product/12345/'
              onMessage={this.handleData.bind(this)}/>
        </div>
      );
    }
  }
 
  export default ProductDetail;

Properties

url

required The url the websocket connection is listening to.

onMessage

required The callback called when data is received. Data is JSON.parse'd

onOpen

The callback called when the connection is successfully opened.

onClose

The callback called when the connection is closed either due to server disconnect or network error.

debug

default: false Set to true to see console logging

reconnect

default: true

accelerated reconnection time

/react-websockets/

    Package Sidebar

    Install

    npm i react-websockets

    Weekly Downloads

    2

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    225 kB

    Total Files

    10

    Last publish

    Collaborators

    • alexander.khivrych