78. Realtime Applications With Django w/ Iacopo Spalletti PyCon Italia

September 4, 2018

Today on The Rabbit Hole we give you Part 2 of Dave’s adventures to PyCon Italia. While over there giving his speech, Dave interviewed Iacopo Spalletti, the Founder and CTO of Nephila, an open source and Django web company based in Florence, London, and Zürich. Iacopo has been a Django developer since the early days of Django 1.0 and is a really passionate organizer and teacher of people in the area for Python. At PyCon Italia, Iacopo’s talk was about building real time applications with Django. Inside this interview, Iacopo goes into detail about Django Channels, web sockets, and the new projects that he is looking forward to. For all this and more, tune in to today’s episode as we dive into the rabbit hole with Iacopo Spalletti.

Key Points From This Episode:

  • Iacopo’s background with Django.
  • How Iacopo stays up to date with the changing industry.
  • Learn more about Django Channels.
  • Introduction of Asynchronous Server Gateway Interface (ASGI).
  • Daphne: the standard Channels protocol server.
  • Limits to be aware of when working with Channels.
  • The limiting factors for using web sockets.
  • New technologies and projects that Iacopo is excited for.
  • And much more!

Transcript for Episode 78. Realtime Applications With Django, With Iacopo Spalletti: PyCon Italia Part 2

[0:00:01.9] MN: Hello and welcome to The Rabbit Hole, the definitive developer’s podcast in fantabulous Chelsey, Manhattan. I’m your host, Michael Nunez. Our co-host today.

[0:00:09.8] DA: Dave Anderson.

[0:00:10.8] MN: Today, we’ve got an interview.

[0:00:13.1] DA: Part two.

[0:00:14.2] MN: Part two. As we mentioned before in time, some time ago, Dave made it to Florence, Italy where they hosted PyCon Italia 2018 and our very own Dave Anderson gave a talk there. What was the title of your talk and how did it go?

[0:00:32.7] DA: Yeah, it was a long title. Kind of a big hook. It was Choose Your Own Adventure for Client Web Services with GraphQL.

[0:00:42.9] MN: Awesome.

[0:00:44.3] DA: I thought it was pretty fun, it was about like all of the adventures you go on when you make a web application and all the ways that you can die a horrible death.

[0:00:54.8] MN: Gosh.

[0:00:56.7] DA: Too many rest end points or whatever.

[0:00:59.0] MN: I remember those as a kid, I was like well, I died here, going back, I imagine it’s the same way?

[0:01:04.2] DA: Yeah, exactly. It’s just like that. You know, I was worried that giving the talk in Italy, people wouldn’t get the jokes about choose your own adventures, but they had choose your own adventure in Italy too.

[0:01:15.8] MN: Oh yeah, they got the Italian version?

[0:01:17.4] DA: Yeah.

[0:01:18.1] MN: That’s awesome.

[0:01:19.6] DA: Yeah like “Selecta una adventura” or something like that.

[0:01:23.9] MN: Yeah, I mean, I imagine they have many different adventures one can go on in Italy and your adventure there was to Florence. You got a couple of interviews out of that?

[0:01:31.8] DA: Yeah.

[0:01:31.8] MN: Can we talk a little bit about the next interview that you had conducted over there?

[0:01:36.7] DA: Yeah, I got a chance to talk to one of the other speakers at PyCon Italia or PyCon Nova has its known in Florence. His name is Iacopo Spalletti. He is a Django developer since like Django 1.0 and really passionate organizer and teacher of people in the area for Python, big advocate. His talk was about building real time applications with Django.

[0:02:05.8] MN: Awesome. What are some things that we’ll learn today form this interview, if you remember?

[0:02:11.6] DA: Yeah. One of the things I thought I really got out of talking with Iacopo was that since he has been programming Django since Django 1.0 and Python 2.whatever, it was cool talking about like how things have changed so much and now, with real time applications, you know, a lot more things are asynchronous like more like JavaScript express type server, which is like way different from how your normal Django applications are.

You know, building a new standard around that called ASGI and how that interacts with the existing standard and it was pretty interesting I think.

[0:02:53.4] MN: Awesome. Well, let’s get right to it. We’ll play the clip and hopefully you enjoy, Dave, out in Florence Italy, interviewing Iacopo Salletti.

[0:03:05.7] DA: Yup, just bump it on over.

[0:03:07.6] MN: Nice.


[0:03:09.4] DA: How are you doing Iacopo?

[0:03:10.2] IS: I’m very fine, thanks and I’m really happy to be on your podcast.

[0:03:15.3] DA: Thank you. Yeah, before we talk about Channels, you could tell us a little bit about yourself like – I understand that you live in Florence itself?

[0:03:24.7] IS: Yes, yes, I live in Florence, a few kilometers from the conference venue and I’m using Django for 10 year almost, so quite a long time and –

[0:03:35.8] DA: What Django version was it at the time?

[0:03:37.6] IS: 1.0

[0:03:39.0] DA: Okay.

[0:03:39.5] IS: I’m not a pioneer, but yeah.

[0:03:42.2] DA: I mean, it’s not less than one but it’s still pretty good.

[0:03:45.3] IS: But it’s still pretty good. We actually replaced the first Django project we built just one year ago.

[0:03:54.4] DA: It’s still around?

[0:03:56.0] IS: It was still around, yes.

[0:03:57.0] DA: Wow, okay.

[0:03:58.9] IS: We updated and replaced with –

[0:04:00.4] DA: Just sent it to the farm upstate or whatever.

[0:04:03.7] IS: Yes, exactly.

[0:04:05.6] DA: That’s awesome. Yeah. I guess like, it’s probably really exciting to see all these wonderful developments that are happening with Django. Django 2.0 is awesome and Django Channels that you’re talking about yesterday, like really cool.

[0:04:17.0] IS: I try to be to keep myself up to date and for this reason. I also contribute to many open sourced projects because it’s just an excuse to be updated upon new things that is coming, the changes, et cetera.

[0:04:34.1] DA: Yeah, all those shiny new toys, got to keep up. When you showed the demo, so I guess just some context about what Channels is like. From your talk yesterday, Channels is a way to use web sockets but not just web sockets.

[0:04:49.7] IS: Not just web sockets and that’s the interesting part because Channels has been built as basically in asynchronous framework to build asynchronous applications within Django.

[0:05:01.7] DA: Which itself is like inherently synchronous, right?

[0:05:03.8] IS: Yes, exactly. Web sockets is a natural protocol for Channels to implement as in it’s the only protocol we choose in the core actually but you’re free to implement your own protocols. I mean, could we use them for boats, for IOT devices and whatever you feel like or whatever protocol you may need for –

[0:05:27.2] DA: Like a raspberry pie or whatever.

[0:05:28.7] IS: Yeah.

[0:05:28.8] DA: Like you were talking about earlier?

[0:05:30.0] IS: Yes, exactly, something like this. This has been built to be extremely modular, exactly for this reason because HTTP is very defined protocol. So you know, it works that way and you have a single protocol to take care of. So Django is very monolithic in that respect but Channels works in a very different world so it’s been thought from the start as a malware project.

[0:05:59.8] DA: Yeah.

[0:06:00.5] IS: But sockets is natural but you can do a lot of things with it and every box, especially the new version, which defined – Channels is based on a specification called ASGI, which is the synchronous product of WSGI. It’s been designed — the protocol itself has been designed to allow to build chainable applications. Every application can handle, I don’t know, one part of Channels is a protocol router, which basically route the incoming connections to different protocols, according to a protocol to different –

[0:06:44.0] DA: When you say chainable, you don’t just mean like middleware in your standard Django application but like the protocols that they pass to each other.

[0:06:50.6] IS: Yes, the routers themselves is GI applications. So that you can act every block basically behind the protocol server, which is the front end server of the Channels stack. Every component is itself an application so you can actually chain them and create your own routing. I mean, in Django you have, the routing is very linear, You can have multiple applications but they – all the routing is very static I’d say.

In the case of Channels, you have applications that route incoming connections so that you can build complex pipelines according to your needs.

[0:07:37.4] DA: Which, coming back to like the users in the groups that kind of particular design, defined as part of Channels as well?

[0:07:43.0] IS: Yes, yes. Then you have middleware, which are similar to the WSGI middlewares as they are supposed to add things on the incoming connection of more information for example. The standard middlewares in Channels just the user data or cookies or session from HTTP to web sockets.

[0:08:10.5] DA: If I wanted to add this to my current Django application, will it like play nicely with WSGI side by side, same web server and all?

[0:08:18.6] IS: You have two options. You can replace your WSGI server with Daphne, which is a standard Channels protocol server and Daphne has been built to handle both HTTP and web sockets and can handle the protocol upgrade, which is the part of which moves the connection from HTTP to web socket. Because every web socket connections starts as HTTP and then it’s been upgraded to web sockets

[0:08:45.3] DA: Okay, then you get like all of the nice features like getting all of the push messages and whatnot.

[0:08:48.6] IS: Exactly. Or you can keep your old, your standard WSGI server and have Daphne on a running protocol and then on the HTTP server, you map the connections for example, using the path –WS, the path, usually go through Daphne and the other goes to the standard WSGI server so you can actually adapt to your deployment.

[0:09:16.4] DA: Daphne is like pretty much the default choice, right?

[0:09:18.4] IS: Yes. I’m pretty sure that for channel’s one, you have some more options, I don’t think they are being up dated to Channels two. One nice project which has been designed and has been built on ASGI is called [inaudible], if I remember correctly, by Tom [inaudible].


[0:09:40.0] DA: Not Unicorn.

[0:09:40.2] IS: No, [inaudible]. It’s exactly is the counter part of Unicorn into the – which is based on ASGI but it’s not Channels, it’s for known Django project basically by Tom [inaudible].

[0:09:58.2] DA: Okay, right, okay. API star and all that good stuff?

[0:10:02.0] IS: Exactly.

[0:10:04.2] DA: Oh wow, okay, I guess I got to follow that guy a little bit closer since he’s working with cool toys. Yeah, I’m giving a talk on GraphQL tomorrow and I’m really curious about like, how this can be used for subscriptions, which is like a thing that I don’t think is really very well uncommitted in the python tools right now. But like GraphQL allows for the idea of like this kind of asynchronous like data passing as well like through web sockets, but yeah that sounds like it’s — yeah, we’re excited for that.

[0:10:35.0] IS: Yeah, I’m going to work on a project, which will require us to basically, well what I’ll try — not try, I will try to implement this as service worker code running the browser, establishing a web socket connection to the server and basically sending messages through the service working code so that connection we will stay up even if the user controls the browser. Not the browser but tabs.

[0:11:07.7] DA: Right. So that way you can have like kind of a more true like web application experience where — I do think that I often find myself in a similar situation where maybe the requirements are growing so much that I do feel like I’m basically building like a desktop application in the web.

[0:11:28.1] IS: Yeah, you must be aware of the, somehow, the limits.

[0:11:34.2] DA: There are no limits.

[0:11:35.0] IS: Of the scope. Actually, we as a community, as companies working on the web, we are trying to push every new – every feature even old feature from other environments to the web and yet we must be aware that it’s –

[0:11:54.6] DA: Are there any limits to web sockets? Like anything that I shouldn’t use it for or?

[0:11:58.5] IS: Technically speaking, one limit you have to be aware of, especially with Channels, our Channels is special Channels to how Channels works is that you have one connection per consumer. Consumer are the Channels application. The real applications you are going to write, where the stuff happens.

[0:12:18.3] DA: Django models and business logic and stuff like that.

[0:12:21.9] IS: Yeah exactly. Channels as – Channels through the consumers are state full, you basically you have one consumer instance per connection, per basically per tab or we can have more than one connection for each, for the tab actually. You have to be aware that you don’t have to create too many consumers if you have more features, it’s more – it’s better to create a single consumer, which handle all of them because in that case, you have less memory footprint and less connection.

She is really special, the connection limit, if you write a high traffic website, there will be the limiting factor.

[0:13:03.1] DA: Yeah, is there any problem with like caching?

[0:13:05.2] IS: Sorry?

[0:13:05.3] DA: Is there any form of caching like request caching or?

[0:13:07.9] IS: No, because in web sockets, you don’t have caching.

[0:13:10.7] DA: Okay. Don’t worry about it, okay.

[0:13:13.6] IS: But for the –

[0:13:13.9] DA: But if you reduce the consumer then I guess you have –

[0:13:16.9] IS: You have –

[0:13:17.5] DA: One thing taking care of all.

[0:13:20.3] IS: Because the limiting factor in the synchronous world is how many connections it can handle.

[0:13:25.5] DA: Because it’s expensive.

[0:13:26.5] IS: Yeah, they’re expensive somehow. With Channels you can prioritize both the protocol servers instances and the workers, which can still be used even if by default you don’t – you are not required to have multiple workers like in Chinese one.

Because they can run inside Daphne, basically. But yeah, somehow, the number of connections is a limited factor and the memory footprint also is going to be that limiting factor so I say you must be aware of these when building the application.

[0:14:00.2] DA: All right, I’ll try to keep it under control

[0:14:03.9] IS: In my demo, I decided to write three consumers but it was basically for – because it was a demo version, it was easier to break down the features and for the consumers to explain them.

[0:14:16.0] DA: Talk about it, yeah.

[0:14:17.3] IS: Yeah, but instead you should write a single or less consumers than that one per feature.

[0:14:24.5] DA: Okay, it’s more like do as I say, not as I do.

[0:14:27.4] IS: Yeah, exactly.

[0:14:30.1] DA: Cool. What other technologies are you excited about? Is there anything else like [inaudible] looking at these days?

[0:14:38.3] IS: I’m really vertical on Django, so I can speak about Django CMS, which has one of the other projects I am really involved in too. One of the cold developer is a CMS for Django is one of the leading two CMS’s for Django.

[0:14:53.9] DA: Yeah, I’ve definitely heard of it before.

[0:14:56.7] IS: Which is a nice tool we use and we are using it — the reason I like it is because it’s easy to integrate with the rest of the Django ecosystem and it’s a really – actually, the application [inaudible]. The skilled up — sorry, the skilled up version of the applications application will be built, it was implemented in a Django CMS project because we use that for pretty much everything.

[0:15:28.3] DA: That’s your baby.

[0:15:29.1] IS: Yeah, this is our baby, we know it a lot and we can do crazy stuff with it and –

[0:15:35.2] DA: Be really productive, to get things done.

[0:15:37.0] IS: Yes, also, we use it even in places where maybe it was not really safe but it feels weird sometimes for me to use it in some environments, but it works. It works very well. We know it very well, so we can bend it a bit.

[0:15:54.1] DA: Nice. Cool. Yeah, thank you for setting time to talk to me. I’ll definitely like continue bouncing around, enjoy all the talks that you can enjoy because you’re in this room.

[0:16:06.2] IS: Yeah. I’m lucky because there are very interesting talks.

[0:16:10.5] DA: Yeah, absolutely. No matter where you go it seems. Cool.

[0:16:14.4] IS: Thank you for hosting me on the podcast and looking forward to meet you again in the next days.

[0:16:22.0] DA: Sure, definitely. All right, cool.


[0:16:23.1] MN: Follow us now on Twitter @radiofreerabbit so we can keep the conversation going. Like what you hear? Give us a five star review and help developers like you find their way into The Rabbit Hole and never miss an episode, subscribe now however you listen to your favorite podcast. On behalf of our producer extraordinaire, William Jeffries and my amazing co-host, Dave Anderson and me, your host, Michael Nunez, thanks for listening to The Rabbit Hole.

Links and Resources:

The Rabbit Hole on Twitter

PyCon Italia

Iacopo Spalletti



Django Channels