Would you like to learn how to create a “real-time” game with the Django web framework? Here is my Django Channels tutorial on how to create a two-player game of Obstruction with a React front-end.
UPDATE (Aug 2018): This tutorial only covers the previous version of Django Channels and will not work with Channels 2.0. I will put a new tutorial out covering version 2.0 and the latest changes soon.
If you just want to see the code, Git it here.
Django is great web development framework. It’s far and away my “go-to” when starting a new web project, but like all frameworks and languages, it isn’t a fit for all types of apps. For apps that require “real-time” collaboration or interactive single page applications that lean heavily on AJAX or web socket calls, Django can be worked out to handle it with socket.io or even a DDP implementation, but you may regret it as you dig in.
Enter Django Channels
Django Channels is a fairly new project by Andrew Godwin that extends Django to make it easy to work with WebSockets in a way that should already be familiar to those that use Django. Recently, Channels has been adopted as an official Django Project, so it’s here to stay and will continue to see love from the Django core team and community going forward.
So, let’s dig into Channels and make a simple game of Obstruction. What is Obstruction? Well it’s a simple two-player pencil-n-paper game where each player takes turns claiming squares on a grid. When there are no available squares left, the last player to make a move wins the game. You can read more about it here, but it’s not critical for this tutorial.
What we’ll use in this tutorial:
- Django >= 1.9
- Node Package Manager (npm)
- Django Channels
This tutorial also assumes that you have experience working with Django, but not so much with React.