Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. var app = require('http').createServer(),
  2. io = require('socket.io').listen(app),
  3. fs = require('fs'),
  4. mysql = require('mysql'),
  5. connectionsArray = [],
  6. profile_id,
  7. last_count = 0, //this variable is to check previous count value
  8. connection = mysql.createConnection({
  9. host: 'localhost',
  10. user: 'root',
  11. password: '', //put your own mysql pwd
  12. database: 'mrlerp2', //put your database name
  13. port: 3306,
  14. multipleStatements:true}), POLLING_INTERVAL = 2500,pollingTimer;
  15.  
  16. connection.connect(function(err) {
  17. console.log(err);
  18. });
  19. app.listen(8000);
  20. 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+"'";
  21. var query = connection.query(sql),
  22. users = []; // this array will contain the result of our db query
  23. user_count=[];
  24.  
  25. query.on('error', function(err) { // Handle error, and 'end' event will be emitted after this as well
  26. console.log(err);
  27. updateSockets(err,profile_id);
  28. })
  29. .on('result', function(user) { // it fills our array looping on each user row inside the db
  30. users.push(user);
  31. if(typeof user.c !=='undefined'){
  32. user_count.push(user.c);
  33. }
  34. })
  35. .on('end',function(){ // loop on itself only if there are sockets still connected
  36. if (connectionsArray.length) {
  37.  
  38. Object.keys(connectionsArray).forEach(function(v){
  39. pollingTimer = setTimeout(pollingLoop(v), POLLING_INTERVAL);
  40.  
  41. updateSockets(
  42.  
  43. {users: users,
  44. count:user_count},
  45. profile_id
  46. );
  47.  
  48. });
  49.  
  50. }
  51. })
  52. };
  53.  
  54. profile_id = socket.handshake.query.profile_id;
  55.  
  56. if(!connectionsArray.hasOwnProperty(profile_id)){
  57. console.log('A new socket is connected!');
  58. console.log(profile_id); connectionsArray[profile_id]=socket;
  59. }
  60. if (connectionsArray.length>0) {
  61. Object.keys(connectionsArray).forEach(function(v){
  62. if(typeof v !=='undefined'){
  63. pollingLoop(v);
  64. }
  65. });
  66. }
  67. socket.on('update_viewed_notify',function(){ connection.query("update notification set is_read=0"); });
  68. socket.on('destroy_socket',function(data){
  69. socket.on('disconnect', function() {
  70. var socketIndex = connectionsArray.indexOf(socket);
  71. if (socketIndex >= 0) {
  72. connectionsArray.splice(socketIndex, 1);
  73. }
  74. });
  75. });
  76. connectionsArray.push(socket);
  77. });
  78. var updateSockets = function(data,profile_id) { connectionsArray[profile_id].emit('notification', data); };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement