Skip to content
Mathieu edited this page Sep 24, 2015 · 6 revisions

Download & build the latest version of Couchfriends or use our CDN to include the Couchfriends API in your HTML5 game by adding the following code in the <head> of your game.

<script src="http://cdn.couchfriends.com/js/couchfriends.api-latest.js"></script>

Couchfriends api uses the global window.COUCHFRIENDS or COUCHFRIENDS object variable. The following code will connect you to the Couchfriends websocket server.

COUCHFRIENDS.settings.apiKey = '<your couchfriends.com api key>';
COUCHFRIENDS.connect();

Receiving data

Each data that is received from the server is passed through the .on('type', function(){}); callback. This includes connections of new players or movement of players that are already connected. This callback is used when the above connect(); was successful.

For all available functions see API - Callbacks.

COUCHFRIENDS.on('connect', function() {
    console.log('Ready for action!');
});

Sending data

Sending data can be useful to communicate with your connected players or starting a hosting a new game. All data that is send is done though the COUCHFRIENDS.send() function and should always be a JSON object with the following parameters: topic, action, data. This example shows you how to host a new game.

For all available commando's see API - Sending data.

/**
 * Request a new game host.
 *
 * @param {string} topic The type of data to send. e.g. 'game'
 * @param {sting} [action] The sub type/action to send. e.g. 'host'
 * @param {object} [data] Additional data to send.
 */
var jsonData = {
    topic: 'game',
    action: 'host',
    data: { }
};
COUCHFRIENDS.send(jsonData);
Clone this wiki locally