@cocreate/socket-server 1.28.3 → 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,10 @@
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
+
1
8
  ## [1.28.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.28.2...v1.28.3) (2024-02-18)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.28.3",
3
+ "version": "1.29.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -171,53 +171,63 @@ class SocketServer extends EventEmitter {
171
171
 
172
172
 
173
173
  if (!this.clients.has(socket.clientId)) {
174
- this.clients.set(socket.clientId, []);
174
+ this.clients.set(socket.clientId, {});
175
175
 
176
176
  if (!organization.clients)
177
- organization.clients = { [socket.clientId]: [] }
177
+ organization.clients = { [socket.clientId]: {} }
178
178
  else
179
- organization.clients[socket.clientId] = []
179
+ organization.clients[socket.clientId] = {}
180
180
  }
181
181
 
182
- if (!this.sockets.has(socket.id)) {
183
- this.sockets.set(socket.id, socket);
184
- this.clients.get(socket.clientId).push(socket);
185
- if (!organization.clients[socket.clientId])
186
- organization.clients[socket.clientId] = [socket]
187
- else
188
- organization.clients[socket.clientId].push(socket)
189
- }
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
190
189
 
191
190
  if (socket.user_id) {
192
191
  this.emit('userStatus', { socket, host: socket.host, user_id: socket.user_id, clientId: socket.clientId, userStatus: 'on', organization_id });
193
192
  let user = this.users.get(socket.user_id)
194
193
 
195
- if (!Array.isArray(user)) {
194
+ if (!user) {
196
195
  clearTimeout(user)
197
- this.users.set(socket.user_id, [socket])
196
+ this.users.set(socket.user_id, { [socket.id]: socket })
198
197
  } else
199
- user.push(socket)
198
+ user[socket.id] = socket
200
199
  }
201
200
 
202
201
  }
203
202
 
204
203
  get(data) {
205
- 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
+
206
211
  if (data.broadcast !== false) {
207
- let organization = this.organizations.get(data.organization_id)
208
- if (organization) {
209
- const clients = organization.clients
210
- for (let client of Object.keys(clients)) {
211
- if (data.broadcastSender === false && client === data.clientId) continue
212
-
213
- if (data.broadcastClient)
214
- 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])
215
218
  else
216
- sockets.push(...clients[client])
217
- }
219
+ sockets.push(Object.values(clients[client])[0])
220
+ } else
221
+ sockets.push(...Array.from(Object.values(clients[client])))
218
222
  }
219
223
  } else if (data.broadcastSender !== false) {
220
- 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)
221
231
  }
222
232
  return sockets
223
233
  }
@@ -231,16 +241,20 @@ class SocketServer extends EventEmitter {
231
241
  // Check if the client exists
232
242
  if (clients && clients[socket.clientId]) {
233
243
  const client = clients[socket.clientId]
244
+ delete client[socket.id]
245
+
246
+ if (!Object.keys(client).length)
247
+ delete clients[socket.clientId];
234
248
 
235
249
  // Check if the socket exists in the client's sockets
236
- const index = client.findIndex(item => item.id === socket.id);
237
- if (index !== -1) {
238
- client.splice(index, 1);
239
- }
250
+ // const index = client.findIndex(item => item.id === socket.id);
251
+ // if (index !== -1) {
252
+ // client.splice(index, 1);
253
+ // }
240
254
 
241
- if (!client.length) {
242
- delete clients[socket.clientId];
243
- }
255
+ // if (!client.length) {
256
+ // delete clients[socket.clientId];
257
+ // }
244
258
 
245
259
  if (!Object.keys(clients).length) {
246
260
  this.organizations.delete(socket.organization_id);
@@ -265,14 +279,10 @@ class SocketServer extends EventEmitter {
265
279
 
266
280
  if (this.clients.has(socket.clientId)) {
267
281
  const client = this.clients.get(socket.clientId)
268
- const index = client.findIndex(item => item.id === socket.id);
269
- if (index !== -1) {
270
- client.splice(index, 1);
271
- }
282
+ delete client[socket.id]
272
283
 
273
- if (!client.length) {
284
+ if (!Object.keys(client).length)
274
285
  this.clients.delete(socket.clientId);
275
- }
276
286
  }
277
287
 
278
288
  if (this.clients.size === 0) {
@@ -296,12 +306,8 @@ class SocketServer extends EventEmitter {
296
306
  if (socket.user_id) {
297
307
  let sockets = this.users.get(socket.user_id)
298
308
  if (sockets) {
299
- if (Array.isArray(sockets) && sockets.length) {
300
- const index = sockets.findIndex(item => item.id === socket.id);
301
- if (index !== -1) {
302
- sockets.splice(index, 1);
303
- }
304
- } else {
309
+ delete sockets[socket.id]
310
+ if (!Object.keys(sockets).length) {
305
311
  let userDebounceTimer = sockets
306
312
 
307
313
  clearTimeout(userDebounceTimer);
@@ -495,8 +501,8 @@ class SocketServer extends EventEmitter {
495
501
  }
496
502
 
497
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
498
- // if (Data.$filter && Data.$filter.query && Data.$filter.query._id && Data.$filter.query._id.$eq === '$user_id')
499
- // delete Data.$filter.query._id
504
+ if (Data.$filter && Data.$filter.query && Data.$filter.query._id && Data.$filter.query._id.$eq === '$user_id')
505
+ delete Data.$filter.query._id
500
506
 
501
507
 
502
508
  delete Data.socket