Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = require('http').createServer(),
- io = require('socket.io').listen(app),
- fs = require('fs'),
- mysql = require('mysql'),
- connectionsArray = [],
- profile_id,
- last_count = 0, //this variable is to check previous count value
- connection = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: '', //put your own mysql pwd
- database: 'mrlerp2', //put your database name
- port: 3306,
- multipleStatements:true}), POLLING_INTERVAL = 2500,pollingTimer;
- connection.connect(function(err) {
- console.log(err);
- });
- app.listen(8000);
- var pollingLoop = function(profile_id) {var sql="SELECT notify.notification_id,notify_mode.`mode`,notify_verb.verb,notify.msg,tab.name,notify.modifiedon FROM notification AS notify INNER JOIN notification_mode AS notify_mode ON notify_mode.notification_mode_id=notify.`mode` INNER JOIN notification_verb AS notify_verb ON notify_verb.notification_verb_id=notify.verb inner join vtiger_tab as tab on tab.tabid=notify.module where notify.actor!='"+profile_id+"' and notify.userid='"+profile_id+"' ORDER BY modifiedon DESC;select count(*) as c from notification where is_read=1 and userid='"+profile_id+"' and actor!='"+profile_id+"'";
- var query = connection.query(sql),
- users = []; // this array will contain the result of our db query
- user_count=[];
- query.on('error', function(err) { // Handle error, and 'end' event will be emitted after this as well
- console.log(err);
- updateSockets(err,profile_id);
- })
- .on('result', function(user) { // it fills our array looping on each user row inside the db
- users.push(user);
- if(typeof user.c !=='undefined'){
- user_count.push(user.c);
- }
- })
- .on('end',function(){ // loop on itself only if there are sockets still connected
- if (connectionsArray.length) {
- Object.keys(connectionsArray).forEach(function(v){
- pollingTimer = setTimeout(pollingLoop(v), POLLING_INTERVAL);
- updateSockets(
- {users: users,
- count:user_count},
- profile_id
- );
- });
- }
- })
- };
- profile_id = socket.handshake.query.profile_id;
- if(!connectionsArray.hasOwnProperty(profile_id)){
- console.log('A new socket is connected!');
- console.log(profile_id); connectionsArray[profile_id]=socket;
- }
- if (connectionsArray.length>0) {
- Object.keys(connectionsArray).forEach(function(v){
- if(typeof v !=='undefined'){
- pollingLoop(v);
- }
- });
- }
- socket.on('update_viewed_notify',function(){ connection.query("update notification set is_read=0"); });
- socket.on('destroy_socket',function(data){
- socket.on('disconnect', function() {
- var socketIndex = connectionsArray.indexOf(socket);
- if (socketIndex >= 0) {
- connectionsArray.splice(socketIndex, 1);
- }
- });
- });
- connectionsArray.push(socket);
- });
- var updateSockets = function(data,profile_id) { connectionsArray[profile_id].emit('notification', data); };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement