Facebook Instant Games 1v1 Multiplayer using Firebase (Part 2)

Michalis Dobekidis
Level Up Coding
Published in
3 min readJul 23, 2018

--

See part 1 here: Facebook Instant Games 1v1 Multiplayer using Firebase (Part 1)

Photo by Rebecca Oliver on Unsplash

On part one we explained the front-end of the multiplayer feature for a Facebook Instant Game, now let’s see the back-end logic using Firebase Firestore-DB and Functions (a sandboxed NodeJS environment to write backend logic without owning a server)

This is how you can get started:

https://firebase.google.com/docs/functions/get-started?authuser=1

Now assuming that our environment is all setup, let’s write some logic to handle the front-end requests

Photo by Aidan Granberry on Unsplash

First of all here are the imports that I used for my project (you might or might not need them all)

Then here is a little snippet that does user validation (to make sure your users do come from Facebook Instant Games)

You can find your APP_SECRET on the Facebook App Settings page

Now let us make the endpoint for startMatchmaking

With the above we are accepting an http request, we receive the user FB-Id and tag (custom tag) and their signature in order to validate their identity.

The rest of the function just adds those data to the bountyQueueRef collection and creates a document with the user FB-Id as a document name (thus unique). When this is done, the function sends a response to the front-end notifying that all is well and the user is now in queue.

Next we create the findMatch function that does the actual matching between players that are on queue.

Again we receive an http request and we validate the sender. After that we try to match the sender with one of the others on the queue, if the algorithm finds a non-flagged (already matched up with someone) user that is not the sender then it immediately flags the user to make them invisible to the other possible concurrent searches, at the same time it also flags the sender so that they are not picked by other searches either.

Read here, why it is good to use transactions to read/write on the DB. https://firebase.google.com/docs/firestore/manage-data/transactions?authuser=1

The function returns a response with the matched player data back to the sender, which is basically irrelevant because the actual functionality is triggered when these two lines finish executing:

t.set(bountyQueueRef.doc(queueEntry.userFBId), queueEntry, { merge: true }); // store their data again but flaggedt.set(bountyQueueRef.doc(userFBId), data, { merge: true }); // store our data again but flagged

Because of the event listener that we have setup on the front-end addBountyQueueListener on both players. The event listener receives an event when the set operation finishes writing on the database.

Then the front-end then proceeds into connecting with the opponent by using

FBInstant.context.createAsync(userFBId)

As described on Part 1 of this tutorial: https://medium.com/@mixalisdobs/facebook-instant-games-1v1-multiplayer-using-firebase-part-1-30c1f6e32b92

I hope that you find the above information useful, drop me a comment if you want me to explain something better or if you indeed make something by using the described approach!

Enjoy!

Photo by Al x on Unsplash

--

--

Senior software engineer (7linternational.com) with a passion for games (playing and creating). I love the creative part of coding and things that are exciting!