创建一个Node.js事件侦听器,用于在表中插入新的mysql记录时(对于Notification Feed) [英] Creating a Node.js event listener for when there is a new mysql record inserted into table (For Notification Feed)

查看:204
本文介绍了创建一个Node.js事件侦听器,用于在表中插入新的mysql记录时(对于Notification Feed)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Codeigniter中编写一个需要实时功能的PHP Web应用程序。具体来说,我开发的两个实时功能是实时聊天和实时通知Feed。

I am writing a PHP web application in Codeigniter that needs to have real time functionality. Specifically, the two real time features I am developing are real time chatting, and a real time notifications feed.

我能够使用Node.JS和Socket.io为网站(客户端到客户端)创建一个实时聊天,但我在如何去创建一个由节点(数据库到客户端)提供支持的通知系统。

I was able to use Node.JS and Socket.io to create a real time chat for the site(client to client), but I am struggling immensely on how to go about creating a notification system powered by node (database to client).

主要问题:


  1. Is there a way to basically have a node event listen to when new records are inserted into a MySQL database?

Node.js可以执行长轮询来检查是否有新的记录被插入到MySQL数据库中。数据库有新记录吗?如果是这样,使用Node.js进行长轮询比更传统的轮询方法更有效率?

Can Node.js perform long polling to check if the database has new records? If so, would doing long polling with Node.js be more efficient than more traditional polling methods?

非常感谢你。任何例子将高度赞赏!

Thank you so much in advance. Any examples would be highly appreciated!

推荐答案

1 -
这取决于您的数据库和您使用的语言/技术。
您可以使用MongoDB和node.js本身在db中插入记录。

1 - It depends on your database and the language/technology that you use . You can use MongoDB and node.js itself to insert the records in db .

但我假设你使用mysql和php,所以你需要一些类型的标志或...的通知来通知node.js。

But i'm assuming your using mysql and php so you need some kind of trigger of flag or ... to notify node.js .

我有很少关于你的应用程序的信息和它的工作原理,但我做的是,我使用ajax插入记录在db,然后我通知node.js

I have very little information about your app and how it works but what i do is , i use ajax to insert records in the db and then i notify node.js

function new_record()
{
     $.post('<?php echo base_url(); ?>/record/add_newrecord' , {data:data} , function(res){

        if($.trim(res) == 'ok' )
        {
            if (typeof io !== 'undefined') 
            {
                  var socket = io.connect('http://localhost:6666');
                  socket.emit('new_record_ADDED' , { data : data });
            }
        }
}

这部分直接在你的php脚本插入记录后

or you can do this part directly in your php script after inserting the record

        if (typeof io !== 'undefined') 
        {
              var socket = io.connect('http://localhost:6666');
              socket.emit('new_record_ADDED' , { data : data });
        }

2 - 可能没有!

2 - probably no !

这篇关于创建一个Node.js事件侦听器,用于在表中插入新的mysql记录时(对于Notification Feed)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆