Connected Players

Welcome to the Facebook Instant Games SDK Tutorials. This series of tutorials covers many aspects of using the Facebook Instant Games SDK.

In this tutorial we will take a look at connected players using the Facebook Instant Games SDK.

What are Connected Players?

Connected players are other players that are connected to the current player that also play the game. Note that this only includes players that have played over the last 90 days. It is useful to have a list of connected players because you can give the option to the player to play with friends that already play the game, increasing retention because people that play together are far more likely to continue playing in the future. To get a list of connected players you can call getConnectedPlayersAsync():

[sourcecode language=”js”]
FBInstant.player.getConnectedPlayersAsync()
.then(function(players) {
// players contains an array of connected players
}).catch(function(error) {
// Error occurred
});
[/sourcecode]

As with the player and context players you can get details of each player in the players array, by calling functions such as getID(), getName(), getPhoto().
Connected players are very useful for offering the player suggestions of who to play with or against in your game which aids retention.

You can create a context between the current player and any other player by calling createAsync(). Lets take a look at a short example:

[sourcecode language=”js”]
function CreateGame(with_player)
{
FBInstant.context.createAsync(with_player)
.then(function() {
// We are now in a context with the other player
}).catch(function(error) {
// Error occurred
});
}

FBInstant.player.getConnectedPlayersAsync()
.then(function(players) {
CreateGame(players[0]);
}).catch(function(error) {
// Error occurred
});
[/sourcecode]

Leave a Reply