|
1 | | -# react-socket |
2 | | -A React wrapper for socket.io |
| 1 | +react-socket |
| 2 | +============ |
| 3 | + |
| 4 | +[](https://travis-ci.org/coma/react-socket) |
| 5 | +[](http://david-dm.org/coma/react-socket) |
| 6 | +[](http://badge.fury.io/js/react-socket) |
| 7 | + |
| 8 | +A React wrapper for Socket.IO |
| 9 | + |
| 10 | +Usage |
| 11 | +----- |
| 12 | + |
| 13 | +Just mount a socket on one of your components: |
| 14 | + |
| 15 | +```javascript |
| 16 | +var React = require('react'), |
| 17 | + Socket = require('react-socket').Socket; |
| 18 | + |
| 19 | +module.exports = module.exports = React.createClass({ |
| 20 | + render: function () { |
| 21 | + |
| 22 | + return ( |
| 23 | + <div> |
| 24 | + <Socket url="your-socket-endpoint:port?"/> |
| 25 | + </div> |
| 26 | + ); |
| 27 | + } |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +and then start listening to it: |
| 32 | + |
| 33 | +```javascript |
| 34 | +var React = require('react'), |
| 35 | + SocketEvent = require('react-socket').Event; |
| 36 | + |
| 37 | +module.exports = module.exports = React.createClass({ |
| 38 | + onSocketMessage: function (message) { |
| 39 | + |
| 40 | + ... |
| 41 | + }, |
| 42 | + render: function () { |
| 43 | + |
| 44 | + return ( |
| 45 | + <div> |
| 46 | + <SocketEvent name="your-socket-event" callback={ this.onSocketMessage }/> |
| 47 | + </div> |
| 48 | + ); |
| 49 | + } |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +Use the name property to mount more than one socket: |
| 54 | + |
| 55 | +```javascript |
| 56 | +var React = require('react'), |
| 57 | + Socket = require('react-socket'); |
| 58 | + |
| 59 | +module.exports = module.exports = React.createClass({ |
| 60 | + render: function () { |
| 61 | + |
| 62 | + return ( |
| 63 | + <div> |
| 64 | + <Socket.Socket url="/a" name="a"/> |
| 65 | + <Socket.Socket url="/b" name="b"/> |
| 66 | + <Socket.Event socket="a" name="foo" callback={ this.onA }/> |
| 67 | + <Socket.Event socket="b" name="foo" callback={ this.onB }/> |
| 68 | + </div> |
| 69 | + ); |
| 70 | + } |
| 71 | +}); |
| 72 | +``` |
0 commit comments