PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  1var npp  = require('./../protocols/npush-protocol'),
  2	nps  = require('./../stats/npush-stats');
  3/**
  4 * 
  5 * @param {socket.io} io
  6 * @param object worker
  7 */
  8exports.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
 59			if(!worker.allowGlobalTraffic) return;
 60
 61			// set path to room
 62			var _path = '/' + socket.handshake.client_room + '/' + socket.channel + '/' + socket.room + '/';        
 63
 64			// save message in db
 65			npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) { 
 66
 67				// send message
 68				io.sockets.in(socket.path).emit('message', _data);
 69			});
 70		});
 71		socket.on('message-read', function (_msg) {
 72
 73			npp.PusherUserActivityUpdate(worker, socket);		
 74			npp.PusherUserMessageRead(worker, _msg, socket);
 75		});
 76		// BROADCAST MESSAGES
 77		socket.on('message boradcast rooms', function (_msg, fn) {
 78
 79			if(!worker.allowGlobalTraffic) return;
 80
 81			var _path = '/' + socket.client + '/' + socket.channel + '/';
 82
 83			// save message in db
 84			npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) { 
 85
 86				// broadcast to app
 87				_data.dataSystem.type = 1;      
 88
 89				// get all rooms
 90				var rooms = io.sockets.manager.rooms;        
 91
 92				for(room in rooms)
 93					if(room.search(_path) == 0)
 94						io.sockets.in(room.substr(1, room.length)).emit('message', _data);
 95			});
 96		});
 97		socket.on('message broadcast app', function (_msg, fn) {
 98
 99			if(!worker.allowGlobalTraffic) return;
100
101			var _path = '/' + socket.client + '/';
102
103			// save message in db
104			npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) { 
105
106				// broadcast to app
107				_data.dataSystem.type = 3;
108
109				// get all rooms
110				var rooms = io.sockets.manager.rooms;
111
112				for(room in rooms)
113					if(room.search(_path) == 0)
114						io.sockets.in(room.substr(1, room.length)).emit('message', _data);	
115			});
116		});  
117		socket.on('message broadcast channel', function (_channel, _msg, fn) {
118
119			if(!worker.allowGlobalTraffic) return;
120
121			var _path = '/' + socket.client + '/' + _channel + '/';
122
123			// save message in db
124			npp.PusherUserMessageSave(worker, _msg, _path, socket, function(_data) { 
125
126				// broadcast to app
127				_data.dataSystem.type = 2;
128
129				// get all rooms
130				var rooms = io.sockets.manager.rooms; 
131
132				for(room in rooms)
133					if(room.search(_path) == 0)
134						io.sockets.in(room.substr(1, room.length)).emit('message', _data);
135			});
136		});  
137		// READ TIME OF LAST ACTIVITY IN MS FROM DB AND SEND UNREAD MESSAGES
138		socket.on('public_activity_ms', function (_period, fn) {
139
140			if(!worker.allowGlobalTraffic) return;
141
142			var _search_time = ' { "user_id" : "' + socket.user_id + '", "rooms./' + socket.client + '/' + socket.channel + '/' + socket.room + '/.date" : {"$exists": true} } ';
143
144			_search = JSON.parse(_search_time);
145
146			db.collection('act_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
147
148				if (!doc) return;
149
150				var _time = new Date();
151				socket.user_last_activity = _time.getTime() - _period;
152
153				var _time_info = new Date(socket.user_last_activity*1);
154
155				// send info
156				io.sockets.socket(socket.id).emit('announcement', 'activity since ' + _time_info + ' on ' 
157					+ '/' + socket.client + '/' + socket.channel + '/' + socket.room + '/');
158
159				// SEARCH FOR MESSAGES FOR ROOM
160				var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/' + socket.room + '/" } ';				
161				_search = JSON.parse(_search_msg);
162
163				db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
164
165					if (!doc) return;
166
167					io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
168
169					nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
170				});
171
172				// SEARCH FOR MESSAGES FOR CHANNEL
173				var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/" } ';				
174				_search = JSON.parse(_search_msg);
175
176				db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
177
178					if (!doc) return;
179
180					io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
181
182					nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
183				});
184
185				// SEARCH FOR MESSAGES FOR APP
186				var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/" } ';				
187				_search = JSON.parse(_search_msg);
188
189				db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
190
191					if (!doc) return;
192
193					io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
194
195					nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
196				});		
197			});
198		});  	
199		// READ TIME OF LAST ACTIVITY FROM DB AND SEND UNREAD MESSAGES
200		socket.on('public_activity', function (nick, fn) {
201
202			if(!worker.allowGlobalTraffic) return;
203
204			var _search_time = ' { "user_id" : "' + socket.user_id + '", "rooms./' + socket.client + '/' + socket.channel + '/' + socket.room + '/.date" : {"$exists": true} } ';
205
206			search = JSON.parse(_search_time);
207
208			db.collection('act_' + socket.handshake.client_room).find(search).forEach(function(err, doc) {
209
210				if (!doc) return;
211
212				socket.user_last_activity = doc['rooms']['/' + socket.client + '/' + socket.channel + '/' + socket.room + '/']['date'];
213				var _time = new Date(socket.user_last_activity*1);
214
215				// send info
216				io.sockets.socket(socket.id).emit('announcement', 'last activity ' + _time + ' on ' 
217					+ '/' + socket.client + '/' + socket.channel + '/' + socket.room + '/');
218
219				// SEARCH FOR MESSAGES FOR ROOM
220				var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/' + socket.room + '/" } ';				
221				_search = JSON.parse(_search_msg);
222
223				db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
224
225					if (!doc) return;
226
227					io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
228
229					nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
230				});
231
232				// SEARCH FOR MESSAGES FOR CHANNEL
233				var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/' + socket.channel + '/" } ';				
234				_search = JSON.parse(_search_msg);
235
236				db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
237
238					if (!doc) return;
239
240					io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
241
242					nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
243				});
244
245				// SEARCH FOR MESSAGES FOR APP
246				var _search_msg = ' { "date" : { "$gt" : ' + socket.user_last_activity + ' }, "room" : "/' + socket.client + '/" } ';				
247				_search = JSON.parse(_search_msg);
248
249				db.collection('msg_' + socket.handshake.client_room).find(_search).forEach(function(err, doc) {
250
251					if (!doc) return;
252
253					io.sockets.socket(socket.id).emit('message', npp.PusherMakeMessageFromDB(doc));
254
255					nps.PusherAPIsActivity(worker, socket, 1, 1, 0, 0);
256				});
257			});
258		});
259		// SERVER CONTROLL COMMAND
260		socket.on('system', function(data) {
261			process.send({
262				event:'system-command', data:data});
263		});
264		// DISCONNECT AND SAVE TIME OF EVENT IN DB
265		socket.on('disconnect', function () {
266			
267			// delete user from list
268			if (!socket.nickname) return;
269
270			npp.PusherUserActivityUpdate(worker, socket);
271
272			try {
273				// delete user_id from array    
274				delete worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room][socket.user_id];
275
276				// delete room space in array if no user exist
277				if(Object.keys(worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room]).length == 0)
278					delete worker.nicknames[socket.handshake.client_room][socket.handshake.channel][socket.handshake.user_room];
279
280				// delete channe; space in array if no channel exist
281				if(Object.keys(worker.nicknames[socket.handshake.client_room][socket.handshake.channel]).length == 0)
282					delete worker.nicknames[socket.handshake.client_room][socket.handshake.channel];
283
284				// delete channe; space in array if no channel exist
285				if(Object.keys(worker.nicknames[socket.handshake.client_room]).length == 0) {
286
287					delete worker.nicknames[socket.handshake.client_room];
288					delete worker.activity[socket.handshake.client_room];
289
290				} else {
291
292					// set info in activity object - list will be send to watchers
293					worker.activity[socket.client].c.s = true;		
294					// update connected user in socket -> as is active-sockets
295					worker.activity[socket.client].n.as--;						
296				}
297			} catch(e) {
298				console.log('error on disconnect: ' + e);
299			}
300		});
301	});		
302}