@cocreate/socket-server 1.28.2 → 1.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.29.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.28.3...v1.29.0) (2024-03-18)
2
+
3
+
4
+ ### Features
5
+
6
+ * get socket using socket.id to maintain a persitient socket connection wtih client tabs ([922a462](https://github.com/CoCreate-app/CoCreate-socket-server/commit/922a4629573751ca87e72fde46598bc340af419f))
7
+
8
+ ## [1.28.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.28.2...v1.28.3) (2024-02-18)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * add host ([7542ee7](https://github.com/CoCreate-app/CoCreate-socket-server/commit/7542ee7cb28821515078a912e40574aa0c761b00))
14
+
1
15
  ## [1.28.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.28.1...v1.28.2) (2024-02-15)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.28.2",
3
+ "version": "1.29.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -73,6 +73,7 @@ class SocketServer extends EventEmitter {
73
73
  let data = {
74
74
  socket,
75
75
  method: 'object.read',
76
+ host: socket.host,
76
77
  array: 'message_log',
77
78
  $filter: {
78
79
  sort: [
@@ -95,7 +96,7 @@ class SocketServer extends EventEmitter {
95
96
 
96
97
  if (self.authenticate) {
97
98
  const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
98
- const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
99
+ const userStatus = { socket, method: 'userStatus', host: socket.host, user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
99
100
  if (user_id) {
100
101
  options.user_id = user_id
101
102
  socket.user_id = user_id;
@@ -170,53 +171,63 @@ class SocketServer extends EventEmitter {
170
171
 
171
172
 
172
173
  if (!this.clients.has(socket.clientId)) {
173
- this.clients.set(socket.clientId, []);
174
+ this.clients.set(socket.clientId, {});
174
175
 
175
176
  if (!organization.clients)
176
- organization.clients = { [socket.clientId]: [] }
177
+ organization.clients = { [socket.clientId]: {} }
177
178
  else
178
- organization.clients[socket.clientId] = []
179
+ organization.clients[socket.clientId] = {}
179
180
  }
180
181
 
181
- if (!this.sockets.has(socket.id)) {
182
- this.sockets.set(socket.id, socket);
183
- this.clients.get(socket.clientId).push(socket);
184
- if (!organization.clients[socket.clientId])
185
- organization.clients[socket.clientId] = [socket]
186
- else
187
- organization.clients[socket.clientId].push(socket)
188
- }
182
+
183
+ this.sockets.set(socket.id, socket);
184
+ this.clients.get(socket.clientId)[socket.id] = socket
185
+ if (!organization.clients[socket.clientId])
186
+ organization.clients[socket.clientId] = { [socket.id]: socket }
187
+ else
188
+ organization.clients[socket.clientId][socket.id] = socket
189
189
 
190
190
  if (socket.user_id) {
191
- this.emit('userStatus', { socket, user_id: socket.user_id, clientId: socket.clientId, userStatus: 'on', organization_id });
191
+ this.emit('userStatus', { socket, host: socket.host, user_id: socket.user_id, clientId: socket.clientId, userStatus: 'on', organization_id });
192
192
  let user = this.users.get(socket.user_id)
193
193
 
194
- if (!Array.isArray(user)) {
194
+ if (!user) {
195
195
  clearTimeout(user)
196
- this.users.set(socket.user_id, [socket])
196
+ this.users.set(socket.user_id, { [socket.id]: socket })
197
197
  } else
198
- user.push(socket)
198
+ user[socket.id] = socket
199
199
  }
200
200
 
201
201
  }
202
202
 
203
203
  get(data) {
204
- let sockets = []
204
+ let sockets = [], clients
205
+ let organization = this.organizations.get(data.organization_id)
206
+ if (organization)
207
+ clients = organization.clients
208
+ else
209
+ return []
210
+
205
211
  if (data.broadcast !== false) {
206
- let organization = this.organizations.get(data.organization_id)
207
- if (organization) {
208
- const clients = organization.clients
209
- for (let client of Object.keys(clients)) {
210
- if (data.broadcastSender === false && client === data.clientId) continue
211
-
212
- if (data.broadcastClient)
213
- sockets.push(clients[client][0])
212
+ for (let client of Object.keys(clients)) {
213
+ if (data.broadcastSender === false && client === data.clientId) continue
214
+
215
+ if (data.broadcastClient) {
216
+ if (client === data.clientId && clients[client][data.socket.id])
217
+ sockets.push(clients[client][data.socket.id])
214
218
  else
215
- sockets.push(...clients[client])
216
- }
219
+ sockets.push(Object.values(clients[client])[0])
220
+ } else
221
+ sockets.push(...Array.from(Object.values(clients[client])))
217
222
  }
218
223
  } else if (data.broadcastSender !== false) {
219
- sockets.push(data.socket)
224
+ if (clients[data.clientId]) {
225
+ if (clients[data.clientId][data.socket.id])
226
+ sockets.push(clients[data.clientId][data.socket.id])
227
+ else
228
+ sockets.push(Object.values(clients[data.clientId])[0])
229
+ } else
230
+ sockets.push(data.socket)
220
231
  }
221
232
  return sockets
222
233
  }
@@ -230,16 +241,20 @@ class SocketServer extends EventEmitter {
230
241
  // Check if the client exists
231
242
  if (clients && clients[socket.clientId]) {
232
243
  const client = clients[socket.clientId]
244
+ delete client[socket.id]
245
+
246
+ if (!Object.keys(client).length)
247
+ delete clients[socket.clientId];
233
248
 
234
249
  // Check if the socket exists in the client's sockets
235
- const index = client.findIndex(item => item.id === socket.id);
236
- if (index !== -1) {
237
- client.splice(index, 1);
238
- }
250
+ // const index = client.findIndex(item => item.id === socket.id);
251
+ // if (index !== -1) {
252
+ // client.splice(index, 1);
253
+ // }
239
254
 
240
- if (!client.length) {
241
- delete clients[socket.clientId];
242
- }
255
+ // if (!client.length) {
256
+ // delete clients[socket.clientId];
257
+ // }
243
258
 
244
259
  if (!Object.keys(clients).length) {
245
260
  this.organizations.delete(socket.organization_id);
@@ -264,14 +279,10 @@ class SocketServer extends EventEmitter {
264
279
 
265
280
  if (this.clients.has(socket.clientId)) {
266
281
  const client = this.clients.get(socket.clientId)
267
- const index = client.findIndex(item => item.id === socket.id);
268
- if (index !== -1) {
269
- client.splice(index, 1);
270
- }
282
+ delete client[socket.id]
271
283
 
272
- if (!client.length) {
284
+ if (!Object.keys(client).length)
273
285
  this.clients.delete(socket.clientId);
274
- }
275
286
  }
276
287
 
277
288
  if (this.clients.size === 0) {
@@ -295,18 +306,14 @@ class SocketServer extends EventEmitter {
295
306
  if (socket.user_id) {
296
307
  let sockets = this.users.get(socket.user_id)
297
308
  if (sockets) {
298
- if (Array.isArray(sockets) && sockets.length) {
299
- const index = sockets.findIndex(item => item.id === socket.id);
300
- if (index !== -1) {
301
- sockets.splice(index, 1);
302
- }
303
- } else {
309
+ delete sockets[socket.id]
310
+ if (!Object.keys(sockets).length) {
304
311
  let userDebounceTimer = sockets
305
312
 
306
313
  clearTimeout(userDebounceTimer);
307
314
  userDebounceTimer = setTimeout(() => {
308
315
  this.users.delete(socket.user_id);
309
- this.emit('userStatus', { socket, user_id: socket.user_id, clientId: socket.clientId, userStatus: 'off', organization_id });
316
+ this.emit('userStatus', { socket, user_id: socket.user_id, host: socket.host, clientId: socket.clientId, userStatus: 'off', organization_id });
310
317
  }, 10000);
311
318
 
312
319
  this.users.set(socket.user_id, userDebounceTimer)
@@ -356,7 +363,7 @@ class SocketServer extends EventEmitter {
356
363
  // data.error = 'Token expired'
357
364
  // socket.send(JSON.stringify(data))
358
365
  await this.send({
359
- socket, method: 'updateUserStatus', user_id: socket.user_id, clientId: data.clientId, userStatus: 'off', socketId: data.socketId, organization_id
366
+ socket, method: 'updateUserStatus', user_id: socket.user_id, host: socket.host, clientId: data.clientId, userStatus: 'off', socketId: data.socketId, organization_id
360
367
  })
361
368
  socket.user_id = socket.expires = null
362
369
  return
@@ -493,6 +500,11 @@ class SocketServer extends EventEmitter {
493
500
  Data = { ...data }
494
501
  }
495
502
 
503
+ // TODO: the following code can cause issues in client and improved approach is to check if user has permission and send or dont send
504
+ if (Data.$filter && Data.$filter.query && Data.$filter.query._id && Data.$filter.query._id.$eq === '$user_id')
505
+ delete Data.$filter.query._id
506
+
507
+
496
508
  delete Data.socket
497
509
  sockets[i].send(JSON.stringify(Data));
498
510