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.
我不是在寻找有人来编写答案的代码,但我想知道是否有人可以概述如何将Django与node.js一起使用以获得即时的,事件驱动的更新。
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.
我有各种各样的“新闻提要”,我希望在新项目进入时更新该Feed。我的一般想法是通过javascript打开与节点服务器的连接,让节点服务器“睡觉“直到有一个新项目进入,此时它返回数据并且Django端启动另一个连接。
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?
当我真正编写node.js代码时,我的困惑就出现了 - 文档显示了很多信息,但没有显示如何使用这些信息。我很感激,如果有人可以指导我去寻找这个 - 我是否使用像EventEmitter这样的东西?如何让Django端脚本要求节点服务器只监听有关特定用户的事件?
Thanks in advance!
提前致谢!
2 个解决方案
#1
7
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,
当来自Django的请求进来时,
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:
该请求将通过JavaScript进行$ .ajax()调用,该调用本质上是长时间轮询并等到发生了什么。当服务器上发生某些事情,即用户做了某事时,服务器将发布到节点。该请求将导致发出事件:
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.
...导致第二个代码段的回调函数被调用。然后它找到与该请求关联的响应对象,返回信息,并由Django端脚本解析。
Hope this helps someone, thought I'd just post my answer.
希望这有助于某人,以为我只是发布我的答案。
#2
2
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.
如果您对编写node.js代码感到不舒服,那么您应该查看基于Python的服务器,这些服务器专门用于支持长时间运行的HTTP请求。
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!
龙卷风就是其中之一,您可以在其中运行Django(例如http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/)。它的CPU /内存使用率可能不如node.js那么出色,但它绝对值得一看!
#1
7
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,
当来自Django的请求进来时,
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:
该请求将通过JavaScript进行$ .ajax()调用,该调用本质上是长时间轮询并等到发生了什么。当服务器上发生某些事情,即用户做了某事时,服务器将发布到节点。该请求将导致发出事件:
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.
...导致第二个代码段的回调函数被调用。然后它找到与该请求关联的响应对象,返回信息,并由Django端脚本解析。
Hope this helps someone, thought I'd just post my answer.
希望这有助于某人,以为我只是发布我的答案。
#2
2
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.
如果您对编写node.js代码感到不舒服,那么您应该查看基于Python的服务器,这些服务器专门用于支持长时间运行的HTTP请求。
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!
龙卷风就是其中之一,您可以在其中运行Django(例如http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/)。它的CPU /内存使用率可能不如node.js那么出色,但它绝对值得一看!