Skip to content

Commit

Permalink
chore(websocket): performance
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jan 11, 2019
1 parent 9400d6b commit 0d4ed8f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 59 deletions.
2 changes: 0 additions & 2 deletions src/public/js/modules/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ define('modules/chat',[
}

chatClient.bindActions();
socket.emit('$trudesk:chat:updateOnlineBubbles');

});

socket.removeAllListeners('$trudesk:chat:updateOnlineBubbles');
Expand Down
4 changes: 2 additions & 2 deletions src/public/js/modules/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ define('modules/ui', [
btnMailNotifications.on('click', updateMailNotificationsClicked);
});

socket.removeAllListeners('$trudesk:chat:updateConversationsNotifications');
socket.on('$trudesk:chat:updateConversationsNotifications', function(data) {
socket.removeAllListeners('updateConversationsNotifications');
socket.on('updateConversationsNotifications', function(data) {
var label = $('#btn_mail-notifications').find('> span');
//TODO: Fixed this once unread messages is fully impl.
var count = 0; // Setting this to 0 to clear label until above is impl.
Expand Down
98 changes: 49 additions & 49 deletions src/socketio/chatSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,27 @@ function register(socket) {
if (socket.request.user.logged_in)
joinChatServer(socket);

// var socketInterval = setInterval(function() {
// console.log('CALLED');
// updateConversationsNotifications(socket);
// }, 5000);
// var a = [];
// a.push(socketInterval);
}

function registerInterval() {
// Global to server start (1 instance)
setInterval(function() {
return setInterval(function() {
updateUsers();
updateOnlineBubbles();
updateConversationsNotifications();
}, 5000);
}

function updateUsers() {
var sortedUserList = sharedUtils.sortByKeys(sharedVars.usersOnline);
utils.sendToAllConnectedClients(io, 'updateUsers', sortedUserList);
}

function updateOnlineBubbles() {
var sortedUserList = _.fromPairs(_.sortBy(_.toPairs(sharedVars.usersOnline), function(o) { return o[0]; }));
var sortedIdleList = _.fromPairs(_.sortBy(_.toPairs(sharedVars.idleUsers), function(o) { return o[0]; }));

utils.sendToAllConnectedClients(io, '$trudesk:chat:updateOnlineBubbles', {sortedUserList: sortedUserList, sortedIdleList: sortedIdleList});

// utils.sendToSelf(socket, '$trudesk:chat:updateOnlineBubbles', {sortedUserList: sortedUserList, sortedIdleList: sortedIdleList});
}

events.updateOnlineBubbles = function(socket) {
Expand All @@ -64,66 +63,67 @@ events.updateOnlineBubbles = function(socket) {
});
};

function updateConversationsNotifications(socket) {
async.each(io.sockets, function(sock) {
console.log(sock);
});
function updateConversationsNotifications() {
_.each(io.sockets.sockets, function(socket) {
if (!socket.request && !socket.request.user)
return;

var userId = socket.request.user._id;
var messageSchema = require('../models/chat/message');
var conversationSchema = require('../models/chat/conversation');
conversationSchema.getConversationsWithLimit(userId, 10, function(err, conversations) {
if (err) {
winston.warn(err.message);
return false;
}
var userId = socket.request.user._id;
var messageSchema = require('../models/chat/message');
var conversationSchema = require('../models/chat/conversation');
conversationSchema.getConversationsWithLimit(userId, 10, function(err, conversations) {
if (err) {
winston.warn(err.message);
return false;
}

var convos = [];
var convos = [];

async.eachSeries(conversations, function(convo, done) {
var c = convo.toObject();
async.eachSeries(conversations, function(convo, done) {
var c = convo.toObject();

var userMeta = convo.userMeta[_.findIndex(convo.userMeta, function(item) { return item.userId.toString() === userId.toString(); })];
if (!_.isUndefined(userMeta) && !_.isUndefined(userMeta.deletedAt) && userMeta.deletedAt > convo.updatedAt)
return done();
var userMeta = convo.userMeta[_.findIndex(convo.userMeta, function(item) { return item.userId.toString() === userId.toString(); })];
if (!_.isUndefined(userMeta) && !_.isUndefined(userMeta.deletedAt) && userMeta.deletedAt > convo.updatedAt)
return done();


messageSchema.getMostRecentMessage(c._id, function(err, rm) {
if (err) return done(err);
messageSchema.getMostRecentMessage(c._id, function(err, rm) {
if (err) return done(err);

_.each(c.participants, function(p) {
if (p._id.toString() !== userId.toString())
c.partner = p;
});
_.each(c.participants, function(p) {
if (p._id.toString() !== userId.toString())
c.partner = p;
});

rm = _.first(rm);
rm = _.first(rm);

if (!_.isUndefined(rm)) {
if (!c.partner || !rm.owner) return done();
if (!_.isUndefined(rm)) {
if (!c.partner || !rm.owner) return done();

if (String(c.partner._id) === String(rm.owner._id))
c.recentMessage = c.partner.fullname + ': ' + rm.body;
else
c.recentMessage = 'You: ' + rm.body;
if (String(c.partner._id) === String(rm.owner._id))
c.recentMessage = c.partner.fullname + ': ' + rm.body;
else
c.recentMessage = 'You: ' + rm.body;

} else
c.recentMessage = 'New Conversation';
} else
c.recentMessage = 'New Conversation';


convos.push(c);
convos.push(c);

return done();
});
return done();
});

}, function(err) {
if (err) return false;
return utils.sendToSelf(socket, 'updateConversationsNotifications', {conversations: convos});
}, function(err) {
if (err) return false;
return utils.sendToSelf(socket, 'updateConversationsNotifications', {conversations: convos});
});
});
});
}

events.updateConversationsNotifications = function(socket) {
socket.on('$trudesk:chat:updateConversationsNotifications', function() {
socket.on('updateConversationsNotifications', function() {
updateConversationsNotifications(socket);
});
};
Expand Down
6 changes: 0 additions & 6 deletions src/socketserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ var socketServer = function(ws) {
notificationSocket.register(socket);
backuprestore.register(socket);

socket.on('updateUsers', function() {
var sortedUserList = sharedUtils.sortByKeys(sharedVars.usersOnline);

utils.sendToSelf(socket, 'updateUsers', sortedUserList);
});

socket.on('ticket:updategrid', function() {
utils.sendToAllConnectedClients(io, 'ticket:updategrid');
});
Expand Down

0 comments on commit 0d4ed8f

Please sign in to comment.