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.
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).
Main Questions:
Is there a way to basically have a node event listen to when new records are inserted into a MySQL database?
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 -
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 .
But i'm assuming your using mysql and php so you need some kind of trigger of flag or ... to notify 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 });
}
}
}
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 - probably no !