My Hexaville
Spead the Word
andorid tic tac toe multiplayer firebase

Android Tic Tac Toe Wi-Fi Multiplayer with Firebase

This is the final part, I have even more stuff done here. Finalized single mode, added Firebase sign up, list of users, invite and actual Wi-Fi game

Firebase Sign Up

I chose Firebase because it requires very least of writing backend code. Although it’s possible to implement it with any type of backend. As for login email/password just because having Facebook will require SHA-1 verification, which means that everybody will have to create their own Facebook projects.


Adding push notifications is really easy with Firebase Push Notifications. Basically you store push id in Firebase for each use user and using that you can send push to anybody. That what actual invite looks like

andorid tic tac toe multiplayer firebase

This one pops up on top of the screen if it’s on like when somebody calls you. You can do it by setting high priority and adding vibration/ringtone to it. Same push appears in notification bar. Definitely check out this post as an introduction to push notifications with Firebase, one of the easiest way up there

WI-Fi Multiplayer

Here’s the best part. And you won’t beleive how many extra lines of code it took for implementing Wi-Fi multiplayer. Ready? Less that 30. And if you’re familiar with Firebase it’s great.

andorid tic tac toe multiplayer with firebase

It took just few things. First I created node games, each id is id of person who invites – other person’s id. And field actions look like this : 0_0, 2_1. Where first is row, second column.

With Firebase there’s a greate method called addChildEventListener

and inside onChildAdded you can get a key – row/column and value (int) 1 – x, 2 – o. Which is how I declared my shapes, very convenient for converting. onChildAdded is called only once for each column and turn always starts with person who invited.


As you saw above, there’s a restart field with a timestamp. Basically it goes for the same listener. I could replace value with boolean though. So if key is restart , mean somebody clicked restart button and we start a new game.

Now if you noticed, game id will be same as for previous. We just set whole game value to null on start to clear field up. This works both for restart and even if somebody leaves game in the middle of it. The next time those people start a new game fields will be cleaned up before starting it.

And here’s wifi handler

And that’s it as for actual Wi-Fi multiplayer handling code. The easiest way, really. My first Tic Tac Toe I created with App Engine and it’s way more code there. Firebase is really great for realtime kind of features in your app.


Thanks for reading, you can get source code here. You can download it and play right away, test it. There could be few bugs for sure. And of course don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends if you think they will benefit from it


About the Author Ihor Klimov

Formerly an Android developer, lately picked up some Flutter. This blog is everything that I find exciting about Android and Flutter development. Stay tuned and hope to see you again!

follow me on: