GitXplorerGitXplorer
p

react-multiplayer

public
41 stars
6 forks
0 issues

Commits

List of commits on branch master.
Unverified
398de3de352f4bd0b97c1574fb1c484945d6253b

Update README.md

ppetehunt committed 9 years ago
Unverified
c9c6deaff3677f1b10a19bf616779fbfb9baf556

react-multiplayer: its alive

ppetehunt committed 11 years ago
Unverified
3624a10e9198c578ca6ffd6462b24e72374618fd

initial pass

ppetehunt committed 11 years ago

README

The README file for this repository.

react-multiplayer

unmaintained

Make your React apps multiplayer! This leverages Firebase to magically make a React component's state shared across multiple users.

Tutorial: multiplayer notepad

Creating multiplayer notepad is very simple. First, create a simple controlled textarea, just like you would with any form in React. We'll use React's two-way binding helpers to save us some typing:

/** @jsx React.DOM */

var App = React.createClass({
  mixins: [React.addons.LinkedStateMixin],
  getInitialState: function() {
    return {text: ''};
  },
  render: function() {
    return <textarea valueLink={this.linkState('text')} />;
  }
});

React.renderComponent(<App />, document.body);

Next, let's make it multiplayer by adding two lines of code.

/** @jsx React.DOM */

var App = React.createClass({
  mixins: [React.addons.LinkedStateMixin, ReactMultiplayer.Mixin],
  getInitialState: function() {
    return {text: ''};
  },
  render: function() {
    return <textarea valueLink={this.linkState('text')} />;
  }
});

ReactMultiplayer.setFirebaseRoot('https://YOUR_ID_HERE.firebase.com/');
React.renderComponent(<App />, document.body);

Bam. You're done.

Extra features

If you want finer control over how state is shared (i.e. multiple chat rooms), override getFirebaseURL():

/** @jsx React.DOM */

var App = React.createClass({
  mixins: [React.addons.LinkedStateMixin, ReactMultiplayer.Mixin],
  getInitialState: function() {
    return {text: ''};
  },
  getFirebaseURL: function() {
    return 'https://YOUR_ID_HERE.firebaseio.com/' + this.props.chatroom;
  },
  render: function() {
    return <textarea valueLink={this.linkState('text')} />;
  }
});

React.renderComponent(<App chatroom="mychat" />, document.body);