PubNub JavaScript APIs


Publish/Subscribe In a Nutshell

PubNub utilizes a Publish/Subscribe model for real-time data streaming and device signaling which lets you establish and maintain persistent socket connections to any device and push data to global audiences in less than ¼ of a second. (API)


How to Play

Tic-Tac-Toe is for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.

This is a realtime multi-player Tic-Tac-Toe on web- You and your opponent need to play together on separate browsers. You can just open an another browser window to act as your own opponent. Your opponent needs to use the same Game ID by passing it in the URL as a query string.

You (X):

  1. Stay on this browser
  2. Play when your opponent is ready

Your opponent (O):

  1. Open an another browser tab or window.
  2. In the new browser tab or window, go to:

To demonstrate how PubNub publish / subscribe works, scroll a bit to see the bottom section of this page, and watch what message is being published and subscribed!

Game ID:
It is your turn!

Publish/Subscribe Explained

Let's say, you are playing X.

Each time you click one of the cells, your move is published to PubNub data stream. Your opponent receives the data by subscribing the message just have been published.
The UI on both browser windows is updated upon each subscribe call.

You are publishing (your move):
{player: ---, position: ---}

You subscribing (all the messages published to PubNub stream):
{player: ---, position: ---}