One of my favourite icebreakers is to play a game called Once Upon a Time, which is popular enough to have its own card game. When playing, friends get to write one line of a story at a time and each player can only see the one immediately before theirs. The results are always hilarious as stories go off in all kinds of directions. I wanted the ability to play anytime with friends, where all they need is an internet-connected device. In this post I’ll show you how to create a basic online version of the game using Twilio Programmable Chat.

Our tools

Setting up our project

The first thing to do is download the Node.js starter app from the Twilio Docs. Follow the Node.js instructions to get it running – this will lead you through configuring your account information and installing your project dependencies. Once you’re all done, run npm install in your terminal and let’s get rolling!

Looking at the sample project

Here’s the important files in the sample project:

src/token_generator.js – a module for provisioning access tokens to the client

src/router.js – handles user routes, such as /token and /chat/messages (which we’ll create later)

public/chat/index.js – handles all client-side code

Creating an endpoint for our messages

By default, our sample project will get any new messages sent to the server from the time we join the channel. However, any past messages won’t get loaded but fortunately they are stored and can be retrieved in just a few lines of code. Let’s create a new route in src/router.js just before the last line: