The Blog.

Series of tutorials - Deploying your jsPsych experiment on Heroku using Node.js, Express and MongoDB.

UPDATE! The tutorial is live - you can follow the series of blog posts, or head directly to the GitHub repo!

I'm making that tutorial I promised. A series of them, actually.

Lately, I've gotten some requests for a tutorial on deploying jsPsych experiments, so I've committed to writing one. It will be slightly better suited for people with some programming experience, but if you're not familiar with the web and the terminology circulating around the technologies I'm using, please don't be intimidated. I've noticed that most people are turned away by the fact that they have no experience with web development. Maybe you're an R wizard, maybe you have just a bit of experience with programming, maybe you work with JavaScript. Perhaps you'd like to deploy a web experiment, but you're not sure how to set up the back end.

Fret not! There are solutions for you. My intention for this tutorial is to write a solution that is sufficiently simple so that you should be able to deploy your jsPsych experiment with no previous back end experience. Knowing basic JavaScript will help you understand the code, and as always, you might need a good dose of patience and self-initiative - there's a lot you will have to learn on your own, so don't be afraid of googling. If you're a more advanced reader, please don't read too much into my words. The solution is not supposed to be production perfect and I often use simplified language because this tutorial is meant for people who don't necessarily have any experience with the web. That being said, if you have ideas for improvements or find a problem, please contact me and tell me more!

I'll be writing the tutorial on a Linux system. The process should not be very different on a Mac OS, but if you're on Windows, you will have to find out how to install and manage the tools on your own system. I'd recommend installing a virtual machine, which will allow you to run a Linux system inside your Windows OS. Explained in very simple words, a virtual machine used for this purpose is a program that simulates an operating system in its own window - this approach allows you to follow the tutorial step-by-step, word to word, without having to find out how to adjust the installation to a Windows environment. If you want to save yourself from a head ache, get a virtual machine here, or go here to experiment - and to fall in love - with Linux!

What's the deal with having a back end tutorial?

The challenge with jsPsych is that it provides you with client side functionality - what happens on the server is up to you. There is a large number of different solutions and their combinations for deploying your experiment, and this is simply one of them. But for smaller scale experiments, this solution will make deploying very easy, and since it only uses free tiers of cloud hosting and storage services, it will cost you no money.

I'll shortly introduce the technologies we use for this tutorial. If you don't understand the descriptions, don't worry - you'll get an idea of how things work while we're working on the application. You'll need the following ingredients for your secret sauce (I'll tell you where to install them throughout the tutorial):

  1. jsPsych (d'oh!)

    An open source JavaScript library for web based experiments in behavioral sciences.

  2. The Node.js runtime environment and its awesome package manager, NPM

    Node.js® is built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js represents the flesh and bones on top of which we build the back end for our experiment. Express is a web framework for Node.js, and we'll be using Mongoose - a MongoDB driver for Node.js.

  3. MongoDB

    MongoDB is a cross-platform document-oriented NoSQL database. It will work as the storage for the data we get out of jsPsych. If you're interested, you could learn more about NoSQL databases, relational databases or JSON, since that's the format we're getting out of jsPsych. Or you could also not learn about any of those and still be able to complete this tutorial, but you will be none the smarter.

    For more advanced readers - I personally recommend using an ORM between jsPsych and a relational database, as your experimental data can be aggregated in a way that is well suited for a relational DB. I like to make a table for each experiment, passing an identifier I use as a foreign key for the participant every time I make a request for a new page. If you use the relational approach and/or store data between tasks, you'll need to take care of some validation (making sure the participant doesn't repeat the same task several times, that there aren't several entries for the same ID etc), but the partial data gained from storing between the tasks affords you some valuable insight into performance and characteristics of participants who dropped out.

  4. Heroku

    Heroku is a platform as a service (PaaS) that enables developers to build and run applications in the cloud. Heroku has a free tier that suits our needs and offers support for Node.js, so we are going to deploy our application to Heroku - this way, the entire experiment will be contained on the cloud and we do not need to worry about a web server - huzzah!

  5. Git

    Git is a version control tool. In my opinion it's easiest to get started with Git by going to GitHub and doing their Git bootcamp. It might not feel very intuitive at first, but you quickly grow into it, and you won't need to use it very much - the only reason you really need to set it up for this tutorial is that we will need it to deploy the experiment to Heroku.

If you feel inspired and don't have one yet, you should go make a GitHub account right now and learn the basics of Git. It's best to store your experiment in a remote repository and version control has some powerful features that will come in very handy in your development process.