Skip to content

jt416/react-websocket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-websocket: a reactjs websocket component

react-websocket is a simple to use component for ReactJS applications to setup websocket communication. It basically is a very simple wrapper for ws. See https://github.com/websockets/ws

The component renders an empty

element in the DOM. When mounting the component, a websocket connection is opened. The connection will be closed when the component will be unmounted.

Installing

npm install --save react-websocket

Usage

  var React = require('react');
  var Websocket = require('react-websocket');

  var Example = React.createClass({
  
    handleData: function(data) {
       // do something with the data
       this.setState({
          foo: bar
       });
    },
    
    render: function() {
      <Websocket url='ws://localhost:3000/messages'
                 onMessage={this.handleData}/>
    }
  });

Properties

url

required The url the websocket connection is listening to.

onMessage

required The callback function that is called when data has been received.

debug

default: false Set to true to see console logging

About

React websocket component

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%