PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/engines/socket.js

https://bitbucket.org/kris-lab/npush-module
JavaScript | 302 lines | 165 code | 77 blank | 60 comment | 35 complexity | ff2dd15caa8d11068567d6c052a0f7cc MD5 | raw file
  1. var npp = require('./../protocols/npush-protocol'),
  2. nps = require('./../stats/npush-stats');
  3. /**
  4. *
  5. * @param {socket.io} io
  6. * @param object worker
  7. */
  8. exports.createNPushSocketIOEngine = function(io, db, worker) {
  9. /**
  10. * Handle event for each socket. This 'connection' event function is called
  11. * when handshake proccess return true.
  12. *
  13. * Events:
  14. * - message
  15. * - message-read
  16. * - message boradcast rooms
  17. * - message broadcast app
  18. * - message broadcast channel
  19. * - public_activity_ms
  20. * - public_activity
  21. * - disconnect
  22. *
  23. */
  24. io.sockets.on('connection', function (socket) {
  25. // USER LOGIN AND ENVIRONMENT SETTINGS
  26. // SEND WELCOME MESSAGE
  27. io.sockets.socket(socket.id).emit('announcement', socket.handshake.client_wellcome_msg);
  28. // SET USER SOCKET PARAMETERS
  29. socket.api_key = socket.handshake.api_key; // THIS IS API KEY
  30. socket.channel = socket.handshake.channel;
  31. socket.client = socket.handshake.client_room; // THIS IS API KEY
  32. socket.path = socket.handshake.client_room + '/' + socket.handshake.channel + '/' + socket.handshake.user_room;
  33. socket.room = socket.handshake.user_room;
  34. socket.user_id = socket.handshake.user_id;
  35. socket.user_name = socket.handshake.user_name;
  36. // ADD USER TO LIST
  37. if(!worker.nicknames[socket.handshake.client_room]) {
  38. worker.nicknames[socket.handshake.client_room] = {};
  39. }
  40. if(!worker.activity[socket.handshake.client_room]) {
  41. worker.activity[socket.handshake.client_room] = npp.PusherAPIStatsRecordCreate();
  42. }
  43. if(!worker.nicknames[socket.handshake.client_room][socket.handshake.channel]) {
  44. worker.nicknames[socket.handshake.client_room][socket.handshake.channel] = {};
  45. }
  46. if(!worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room]) {
  47. worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room] = {};
  48. }
  49. worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room][socket.handshake.user_id] = socket.nickname = socket.handshake.user_name;
  50. // JOIN ROOM
  51. socket.join(socket.path);
  52. // set info in activity object - list will be send to watchers
  53. worker.activity[socket.client].c.s = true;
  54. // update connected user in socket -> as is active-sockets
  55. worker.activity[socket.client].n.as++;
  56. // USER MESSAGES
  57. socket.on('message', function (_msg) {
  58. if(!worker.allowGlobalTraffic) return;
  59. // set path to room
  60. var _path = '/' + socket.handshake.client_room + '/' + socket.channel + '/' + socket.room + '/';
  61. // save message in db
  62. npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) {
  63. // send message
  64. io.sockets.in(socket.path).emit('message', _data);
  65. });
  66. });
  67. socket.on('message-read', function (_msg) {
  68. npp.PusherUserActivityUpdate(worker, socket);
  69. npp.PusherUserMessageRead(worker, _msg, socket);
  70. });
  71. // BROADCAST MESSAGES
  72. socket.on('message boradcast rooms', function (_msg, fn) {
  73. if(!worker.allowGlobalTraffic) return;
  74. var _path = '/' + socket.client + '/' + socket.channel + '/';
  75. // save message in db
  76. npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) {
  77. // broadcast to app
  78. _data.dataSystem.type = 1;
  79. // get all rooms
  80. var rooms = io.sockets.manager.rooms;
  81. for(room in rooms)
  82. if(room.search(_path) == 0)
  83. io.sockets.in(room.substr(1, room.length)).emit('message', _data);
  84. });
  85. });
  86. socket.on('message broadcast app', function (_msg, fn) {
  87. if(!worker.allowGlobalTraffic) return;
  88. var _path = '/' + socket.client + '/';
  89. // save message in db
  90. npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) {
  91. // broadcast to app
  92. _data.dataSystem.type = 3;
  93. // get all rooms
  94. var rooms = io.sockets.manager.rooms;
  95. for(room in rooms)
  96. if(room.search(_path) == 0)
  97. io.sockets.in(room.substr(1, room.length)).emit('message', _data);
  98. });
  99. });
  100. socket.on('message broadcast channel', function (_channel, _msg, fn) {
  101. if(!worker.allowGlobalTraffic) return;
  102. var _path = '/' + socket.client + '/' + _channel + '/';
  103. // save message in db
  104. npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) {
  105. // broadcast to app
  106. _data.dataSystem.type = 2;
  107. // get all rooms
  108. var rooms = io.sockets.manager.rooms;
  109. for(room in rooms)
  110. if(room.search(_path) == 0)
  111. io.sockets.in(room.substr(1, room.length)).emit('message', _data);
  112. });
  113. });
  114. // READ TIME OF LAST ACTIVITY IN MS FROM DB AND SEND UNREAD MESSAGES
  115. socket.on('public_activity_ms', function (_period, fn) {
  116. if(!worker.allowGlobalTraffic) return;
  117. var _search_time = ' { "user_id" : "' + socket.user_id + '", "rooms./' + socket.client + '/' + socket.channel + '/' + socket.room + '/.date" : {"$exists": true} } ';
  118. _search = JSON.parse(_search_time);
  119. db.collection('act_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
  120. if (!doc) return;
  121. var _time = new Date();
  122. socket.user_last_activity = _time.getTime() - _period;
  123. var _time_info = new Date(socket.user_last_activity*1);
  124. // send info
  125. io.sockets.socket(socket.id).emit('announcement', 'activity since ' + _time_info + ' on '
  126. + '/' + socket.client + '/' + socket.channel + '/' + socket.room + '/');
  127. // SEARCH FOR MESSAGES FOR ROOM
  128. var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/' + socket.room + '/" } ';
  129. _search = JSON.parse(_search_msg);
  130. db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
  131. if (!doc) return;
  132. io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
  133. nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
  134. });
  135. // SEARCH FOR MESSAGES FOR CHANNEL
  136. var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/" } ';
  137. _search = JSON.parse(_search_msg);
  138. db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
  139. if (!doc) return;
  140. io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
  141. nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
  142. });
  143. // SEARCH FOR MESSAGES FOR APP
  144. var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/" } ';
  145. _search = JSON.parse(_search_msg);
  146. db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
  147. if (!doc) return;
  148. io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
  149. nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
  150. });
  151. });
  152. });
  153. // READ TIME OF LAST ACTIVITY FROM DB AND SEND UNREAD MESSAGES
  154. socket.on('public_activity', function (nick, fn) {
  155. if(!worker.allowGlobalTraffic) return;
  156. var _search_time = ' { "user_id" : "' + socket.user_id + '", "rooms./' + socket.client + '/' + socket.channel + '/' + socket.room + '/.date" : {"$exists": true} } ';
  157. search = JSON.parse(_search_time);
  158. db.collection('act_' + socket.handshake.client_room).find(search).forEach(function(err, doc) {
  159. if (!doc) return;
  160. socket.user_last_activity = doc['rooms']['/' + socket.client + '/' + socket.channel + '/' + socket.room + '/']['date'];
  161. var _time = new Date(socket.user_last_activity*1);
  162. // send info
  163. io.sockets.socket(socket.id).emit('announcement', 'last activity ' + _time + ' on '
  164. + '/' + socket.client + '/' + socket.channel + '/' + socket.room + '/');
  165. // SEARCH FOR MESSAGES FOR ROOM
  166. var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/' + socket.room + '/" } ';
  167. _search = JSON.parse(_search_msg);
  168. db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
  169. if (!doc) return;
  170. io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
  171. nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
  172. });
  173. // SEARCH FOR MESSAGES FOR CHANNEL
  174. var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/" } ';
  175. _search = JSON.parse(_search_msg);
  176. db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
  177. if (!doc) return;
  178. io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
  179. nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
  180. });
  181. // SEARCH FOR MESSAGES FOR APP
  182. var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/" } ';
  183. _search = JSON.parse(_search_msg);
  184. db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
  185. if (!doc) return;
  186. io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
  187. nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
  188. });
  189. });
  190. });
  191. // SERVER CONTROLL COMMAND
  192. socket.on('system', function(data) {
  193. process.send({
  194. event:'system-command', data:data});
  195. });
  196. // DISCONNECT AND SAVE TIME OF EVENT IN DB
  197. socket.on('disconnect', function () {
  198. // delete user from list
  199. if (!socket.nickname) return;
  200. npp.PusherUserActivityUpdate(worker, socket);
  201. try {
  202. // delete user_id from array
  203. delete worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room][socket.user_id];
  204. // delete room space in array if no user exist
  205. if(Object.keys(worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room]).length == 0)
  206. delete worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room];
  207. // delete channe; space in array if no channel exist
  208. if(Object.keys(worker.nicknames[socket.handshake.client_room][socket.handshake.channel]).length == 0)
  209. delete worker.nicknames[socket.handshake.client_room][socket.handshake.channel];
  210. // delete channe; space in array if no channel exist
  211. if(Object.keys(worker.nicknames[socket.handshake.client_room]).length == 0) {
  212. delete worker.nicknames[socket.handshake.client_room];
  213. delete worker.activity[socket.handshake.client_room];
  214. } else {
  215. // set info in activity object - list will be send to watchers
  216. worker.activity[socket.client].c.s = true;
  217. // update connected user in socket -> as is active-sockets
  218. worker.activity[socket.client].n.as--;
  219. }
  220. } catch(e) {
  221. console.log('error on disconnect: ' + e);
  222. }
  223. });
  224. });
  225. }