Node.js-监视数据库中的更改 [英] Node.js - Monitoring a database for changes
问题描述
我正在使用node.js服务器在我的Web应用程序和数据库之间创建一个接近实时"的套接字.当前,我正在使用MySQL,它每秒在节点中轮询一次,以检查表是否有任何更改(基于时间戳).
I am using a node.js server to create a 'close to real-time' socket between my web app and a database. Currently I am using MySQL which I am polling every second in node to check if there are any changes to the table (based on a timestamp.)
我想知道是否有使用MySQL进行类似操作的特定技术?目前,我只是在运行SQL查询并在下一次民意调查之前使用setTimeout.
I was wondering if there any specific techniques to doing something like this with MySQL? At the moment, I am just running a SQL query and using setTimeout before the next poll.
我知道在这样的实例中使用NoSQL数据库更为普遍,但是我对这项技术并不满意,我宁愿使用SQL.
I know it's more common to use a NoSQL database in instances like this but I'm not really comfortable with the technology and I'd much rather use SQL.
有人对使用node监视SQL数据库有任何经验或技巧吗?
Does anyone have any experience or tips for monitoring a SQL database with node?
推荐答案
我个人不会为此使用轮询机制.我认为这是pub/sub mq作为数据库顶部组件的一个很好的用例,允许消费者订阅特定渠道以关注他们关心的实体上的更改事件.
I wouldn't personally use a polling mechanism for this. I think this is a pretty good use case for a pub/sub mq as a component on top of the database that allows consumers to subscribe to specific channels for change events on entities that they care about.
例如:
- 有人要求更改模型
- 模型广播更改事件
- 排队更改以将其保留在数据库中
- 触发消息队列中特定频道上的更改集,以分发给所有相关方
您可以使用节点EventEmitter对此类事物使用非常简单的进程内发布/订阅机制,并且在需要扩展,具有持久性要求或需要跨语言MQ时,您可以使用Rabbitmq这样的技术, zeromq等.我已经开始在我的一个应用程序中实现非常轻巧的功能: https://github.com/jmoyers/mettle/blob/master/src/pubsub.coffee
You can use a very simple in process pub/sub mechanism for this type of thing using nodes EventEmitter, and as you need to scale, have durability requirements, or need a cross language MQ you can go to a technology like rabbitmq, zeromq, etc. I've started to implement something very lightweight to do just this in one of my applications: https://github.com/jmoyers/mettle/blob/master/src/pubsub.coffee
它归结为:
pubsub.sub('users.*', function(updates){
// Interested party handles updates for user objects
});
那样,您就不会对数据库施加愚蠢的轮询压力.实际上,变更分布完全独立于写入数据库
That way you aren't putting stupid polling pressure on your database. In fact, change distribution is completely independent of writing to your database
乔什
这篇关于Node.js-监视数据库中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!