Skip to content

collmot/react-socket

Repository files navigation

react-socket

Build StatusDependency StatusNPM version

A React wrapper for Socket.IO

Usage

Just mount a socket on one of your components:

varReact=require('react'),Socket=require('react-socket').Socket;module.exports=module.exports=React.createClass({render: function(){return(<div><Socketurl="your-socket-endpoint:port?"/></div>);}});

and then start listening to it:

varReact=require('react'),SocketEvent=require('react-socket').Event;module.exports=module.exports=React.createClass({onSocketMessage: function(message){ ... },render: function(){return(<div><SocketEventname="your-socket-event"callback={this.onSocketMessage}/></div>);}});

Use the name property to mount more than one socket:

varReact=require('react'),Socket=require('react-socket');module.exports=module.exports=React.createClass({render: function(){return(<div><Socket.Socketurl="/a"name="a"/><Socket.Socketurl="/b"name="b"/><Socket.Eventsocket="a"name="foo"callback={this.onA}/><Socket.Eventsocket="b"name="foo"callback={this.onB}/></div>);}});

Every mounted socket gets disconnect before its component gets unmounted and the same goes to the events.

About

A React wrapper for socket.io

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript100.0%