-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathpresence.ts
More file actions
64 lines (52 loc) · 1.75 KB
/
presence.ts
File metadata and controls
64 lines (52 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Presence } from '@rocket.chat/core-services';
import { InstanceStatus } from '@rocket.chat/instance-status';
import { Accounts } from 'meteor/accounts-base';
import { Meteor } from 'meteor/meteor';
import { throttle } from 'underscore';
// update connections count every 30 seconds
const updateConns = throttle(function _updateConns() {
void InstanceStatus.updateConnections(Meteor.server.sessions.size);
}, 30000);
Meteor.startup(() => {
const nodeId = InstanceStatus.id();
Meteor.onConnection((connection) => {
const session = Meteor.server.sessions.get(connection.id);
connection.onClose(async () => {
if (!session) {
return;
}
await Presence.removeConnection(session.userId, connection.id, nodeId);
updateConns();
});
});
process.on('exit', async () => {
await Presence.removeLostConnections(nodeId);
});
Accounts.onLogin((login: any): void => {
if (!login.connection.id) {
return;
}
// validate if it is a real WS connection and is still open
const session = Meteor.server.sessions.get(login.connection.id);
if (!session) {
return;
}
const _messageReceived = session.heartbeat.messageReceived.bind(session.heartbeat);
session.heartbeat.messageReceived = function messageReceived() {
if (this._seenPacket === false) {
void Presence.updateConnection(login.user._id, login.connection.id).catch((err) => {
console.error('Error updating connection presence on heartbeat:', err);
});
}
return _messageReceived();
};
void (async function () {
await Presence.newConnection(login.user._id, login.connection.id, nodeId);
updateConns();
})();
});
Accounts.onLogout((login): void => {
void Presence.removeConnection(login.user?._id, login.connection.id, nodeId);
updateConns();
});
});