Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Channels does a lot for your app, and so requires a bit of setup to get going. In this video, we’ll get Channels installed, configured, and put a frame in place to start handling websockets events
You can read the Channels documentation. If you'd like a Git repository for the project, here you go!
One of the things that's really
incredible about channels
0:00
is that from the point of view of
developing your Django project,
0:03
it operates just like any
other pluggable Django app.
0:05
The author of channels,
Andrew Godwin, worked really hard to
0:08
find abstractions that would feel
familiar to Django developers, and
0:11
that would play nicely with
the rest of the Django ecosystem.
0:15
Eventually, maybe even by the time
you're watching this workshop,
0:18
channels will be integrated
into the core of Django.
0:21
And then instead of
installing it separately,
0:24
you'll just use it any
time that you need it.
0:27
For now though, we still need to install
channels the way that we would normally
0:29
install any other package.
0:31
So through pip or
in my case through PyCharm.
0:33
Now you can check out a GitHub repository
with all of the code for this workshop.
0:37
I'll have a link to that repository
along with a note on how to check out
0:41
the channel's initial tag.
0:43
So you can code along with
me in the teacher's note.
0:45
On your own computer you'll
probably want to install Django and
0:48
channels into a virtual
environment by using pip.
0:50
Since I'm using Pycharm,
0:53
however, my installation process
is a little bit different.
0:54
So for me I go to the project and
the project interpreter and
0:58
I click the little plus sign and
I choose Django.
1:02
Now I'm in a specified version, 1.10.3.
1:06
You can use 1.10.4 or
whatever version you want to use.
1:09
I'm gonna install that package.
1:14
And then I also need to install channels.
1:16
Now, you'll do this with pip
install channels if you're using
1:19
a virtual environment.
1:21
I'm going to do it, again,
through here, though.
1:22
And I'm going to notice that
this is version 0.17.3.
1:27
I'm not gonna necessarily specify
a version here because, since channels is
1:31
still in development, you probably want to
use the newest one at any given time so
1:35
that you know you have
the newest version of channels.
1:39
And then one thing I definitely
want to remember to do,
1:42
is to add channels to
my requirements .txt.
1:45
So that was 0.17.3 and I will save that.
1:50
Now we have channel installed, we need
to configure our project to use it.
1:55
In the settings.py for the project,
1:59
we're going to add channels
to our installed Apps.
2:02
So I'm gonna add it right
here above my votes app.
2:06
And then down at the bottom of
the settings file, really anywhere but
2:12
I like to add this at the bottom,
we need to add a new set of settings.
2:15
And this is called CHANNEL_LAYERS.
2:21
It's a dictionary and
2:23
the default key is also a dictionary
2:26
which has two keys BACKEND and ROUTING.
2:31
So back end is gonna be
set to A-S-G-I-R-E-F,
2:37
asgiref.inmemory.channellayer.
2:44
And routing is going to be set to
djangocity.routing.channel routing.
2:48
Now this setting is new,
you've likely never seen this before,
2:57
so it might need a little
bit more explaining.
3:01
Channels operates on
an event driven context.
3:03
Events can be messages that you send to
your app to various pieces of channels
3:06
code or they could be client started
events like HTTP requests or
3:09
WebSocket client connections.
3:13
Channels needs a way of keeping track
of events and communicating events
3:15
between different pieces of code and
it uses a channels layer to do so.
3:18
Think of a layer as a messenger
between different pieces of code.
3:23
Channels ships with some
built in layers and
3:26
the one that we're using in this
setting is the in memory layer.
3:29
That layer will keep references and past
messages inside of your system's memory.
3:32
This layer is fine for development but
shouldn't be used in production,
3:37
because it can lead to memory leaks and
degraded performance.
3:39
We'll talk about some other options for
3:42
this when we talk about
deploying our channel's project.
3:44
The last piece of information we
need is the channel's routing,
3:47
which is referenced by the last
line in the channel layers block.
3:50
Channels routes are somewhat like Django's
URL patterns, and live in a similar place.
3:53
Here in the Django City app,
the directory that holds our URLs up high,
3:58
I'm going to add a new file
that's named routing.py.
4:03
And yeah, go ahead and
add that to version control.
4:09
Now this file is going to hold our routes.
4:13
So first I'm going to import from
channels.routing the route function.
4:15
And then I'm gonna make a new
list here named channel_routing,
4:23
much like we would have
a list of url patterns.
4:27
So for now that's it.
4:31
That's all I need to do.
4:33
And I can run the server to make
sure that things are still working.
4:34
So I can do run server and
4:41
we see that we get this busy loop
synchronous mode on channel layer.
4:42
That tells us that
channels are registered.
4:47
And if I refresh this page,
everything still works.
4:50
So our project is now
running with channels.
4:54
We're ready to start
serving WebSockets clients.
4:55
In the next video will add some code
to our project that will respond
4:58
when it receives WebSocket events.
5:01
You need to sign up for Treehouse in order to download course files.
Sign up