Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use node.js for event-based updates in Django?

Tags:

node.js

django

I'm not looking for someone to code the answer, but I was wondering if someone could just give a general overview of how to use Django with node.js in order to get instant, event-driven updates.

I have a "news feed" of sorts, and I'd like for that feed to update when new items come in. The general idea I have is to just open a connection to the node server via javascript, have the node server "sleep" until a new item comes in, at which point it returns the data and the Django-side starts another connection.

My confusion comes when it comes to actually writing node.js code - the documentation shows a lot of information, but not how to use that information. I'd appreciate it if someone could direct me towards where to look for this - do I use something like an EventEmitter? How can I have a Django-side script ask the node server to listen for only events regarding a specific user?

Thanks in advance!

like image 556
munchybunch Avatar asked Jan 23 '11 23:01

munchybunch


People also ask

Can you use NodeJS with Django?

Django is useful for sketching your requirement out, once you know what you are building, you can build the real time data retrieval into NodeJS and let Django handle maintenance functionality such as password reset and also lets you build high-performing, elegant web application quickly and focuses on automating as ...

Is NodeJS event-driven?

By definition, NodeJS is an event-driven non-blocking runtime environment for JavaScript that has become very popular on the server-side. This is because Nodejs has an event-driven architecture capable of asynchronous I/O.

How is event-driven programming implemented in NodeJS?

Event-Driven Programming js uses events heavily and it is also one of the reasons why Node. js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, declares functions and then simply waits for the event to occur.

Is NodeJS faster than Django?

Django gives better performance, owing to a built-in house template system promoting the execution of a vital task promptly. NodeJS performance is also good, as it permits web professionals with more liberty when it comes to implementations.


2 Answers

Nevermind, I think I figured it out. I created my own emitter and had it listen for my own events.

var myemitter = new events.EventEmitter();

And when a request from Django came in,

myemitter.addListener('action'+userid,function(data){
    //do something with data
}

That request would be through a $.ajax() call through JavaScript that would essentially long poll and wait until something happened. When something happened on the server, i.e. the user did something, the server would post to node. The request would cause the event to be emitted:

myemitter.emit('action'+userid,data);

...which causes the callback function from the second code piece to be called. It then finds the response object associated with that request, returns information, and is parsed by the Django-side script.

Hope this helps someone, thought I'd just post my answer.

like image 159
munchybunch Avatar answered Sep 18 '22 23:09

munchybunch


If you're uncomfortable writing node.js code you should have a look at Python-based servers that are build specifically to support long-running HTTP requests.

Tornado is one of them and you can run Django inside it (e.g. http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/ ). Its CPU/memory usage might not be as stellar as node.js, but it's definitely worth a look!

like image 21
Jan Avatar answered Sep 17 '22 23:09

Jan