Tutorial: 01 quick start

01 quick start

Start the server

Multi uses the socket.io library to wire all clients together. You can install it using npm: npm install socket.io. That's all - now you can start multi using node.js.

  // load the server side multi lib
  var multiModule = require('./multi/server');

  // create a new server listening to port 8080
  // this will be used for socket connections
  // you could also use express to create the server
  var server = require('http').createServer().listen(8080);

  // get a Multi instance by passing your server
  var multi = multiModule.init(server);

  // log every newly created session
  multi.on('sessionCreated', function (event) {
    console.log('new session created!', event.session.token);
  });

Create a new session

Once your server is running you can create a new session on the client side:


  // configure where multi can find your client side socket.io lib
  // a running socket.io server will deliver it under 'socket.io/socket.io.js'
  requirejs.config({
    paths: {
      'socket.io': 'http://mySocketioServer:8080/socket.io/socket.io.js'
    }
  });

  // require the client side lib
  // see module:client/multi
  requirejs(['../lib/multi'], function (multiModule) {
    
    // say multi where to find the running socket server
    // via MultiOptions
    var multiOptions = {
      server: 'http://mySocketioServer:8080/'
    };
    
    // init multi with your options 
    // a Multi instance will be returned
    var multi = multiModule.init(multiOptions);
    
    // create a new session with success and error callback
    // see: Multi.createSession
    multi.createSession().then(onSession, onSessionFailed).done();
    
    // receive the Session instance
    function onSession(session) {
      // token you can use to join a session
      console.log(session.token);
    }
    
    // receive an module:shared/errors.MultiError
    // all error types can be found inside the multiModule
    function onSessionFailed(error) {
      console.error(error);
    }
  };

Join an existing session

Now you have created a session (see the server output?). Display its token and join it with another client:


  requirejs.config({
    paths: {
      'socket.io': 'http://mySocketioServer:8080/socket.io/socket.io.js'
    }
  });

  requirejs(['../lib/multi'], function (multiModule) {
    
    var multiOptions = {
      server: 'http://mySocketioServer:8080/'
    };
    var multi = multiModule.init(multiOptions);
    
    // ask user for the session token
    // and pass it to Session.joinSession
    multi.joinSession(token).then(onSession, onSessionFailed).done();
    
    function onSession(session) {
      // display all connected players
      console.log(session.getPlayerArray());
    }
    
    function onSessionFailed(error) {
      console.error(error);
    }
  };

Communicate and listen to events

Once you successfully created or joined a session you can do some fancy stuff:

  // receive the Session instance
  function onSession(session) {

    session.on('playerJoined' function (event) {
      // received a Player instance
      var player = event.player;
      console.log('player added: ' + player.id);
      
      // player attributes will sync to all other clients
      player.attributes.color = '#ff00ff';
      
      // you can wait for these attributes with:
      player.getAttributeAsync('color').then(function (color) {
        console.log('color set:', player.id, color);
      });
      
      // you can subscribe to player events too
      player.on('disconnected', function () {
        console.log('player removed: ' + player.id);
      });
    });
    
    // send session messages to all clients width Session.message
    session.message('startGame', { speed: 50 });
    
    // listen to session events
    session.on('startGame', function (event) {
      console.log(event.data.speed); // 50
    });
  }