@cocreate/socket-server 1.28.3 → 1.29.1

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.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.29.0...v1.29.1) (2024-04-26)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * clearTimout if userid seesion is reconnected ([4895fff](https://github.com/CoCreate-app/CoCreate-socket-server/commit/4895fff37e85eb2cdd3bb985804e8c362268360e))
7
+
8
+ # [1.29.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.28.3...v1.29.0) (2024-03-18)
9
+
10
+
11
+ ### Features
12
+
13
+ * 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))
14
+
1
15
  ## [1.28.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.28.2...v1.28.3) (2024-02-18)
2
16
 
3
17
 
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.1",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -171,53 +171,64 @@ 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) {
195
+ this.users.set(socket.user_id, { [socket.id]: socket })
196
+ } else {
196
197
  clearTimeout(user)
197
- this.users.set(socket.user_id, [socket])
198
- } else
199
- user.push(socket)
198
+ user[socket.id] = socket
199
+ }
200
200
  }
201
201
 
202
202
  }
203
203
 
204
204
  get(data) {
205
- let sockets = []
205
+ let sockets = [], clients
206
+ let organization = this.organizations.get(data.organization_id)
207
+ if (organization)
208
+ clients = organization.clients
209
+ else
210
+ return []
211
+
206
212
  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])
213
+ for (let client of Object.keys(clients)) {
214
+ if (data.broadcastSender === false && client === data.clientId) continue
215
+
216
+ if (data.broadcastClient) {
217
+ if (client === data.clientId && clients[client][data.socket.id])
218
+ sockets.push(clients[client][data.socket.id])
215
219
  else
216
- sockets.push(...clients[client])
217
- }
220
+ sockets.push(Object.values(clients[client])[0])
221
+ } else
222
+ sockets.push(...Array.from(Object.values(clients[client])))
218
223
  }
219
224
  } else if (data.broadcastSender !== false) {
220
- sockets.push(data.socket)
225
+ if (clients[data.clientId]) {
226
+ if (clients[data.clientId][data.socket.id])
227
+ sockets.push(clients[data.clientId][data.socket.id])
228
+ else
229
+ sockets.push(Object.values(clients[data.clientId])[0])
230
+ } else
231
+ sockets.push(data.socket)
221
232
  }
222
233
  return sockets
223
234
  }
@@ -231,16 +242,20 @@ class SocketServer extends EventEmitter {
231
242
  // Check if the client exists
232
243
  if (clients && clients[socket.clientId]) {
233
244
  const client = clients[socket.clientId]
245
+ delete client[socket.id]
246
+
247
+ if (!Object.keys(client).length)
248
+ delete clients[socket.clientId];
234
249
 
235
250
  // 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
- }
251
+ // const index = client.findIndex(item => item.id === socket.id);
252
+ // if (index !== -1) {
253
+ // client.splice(index, 1);
254
+ // }
240
255
 
241
- if (!client.length) {
242
- delete clients[socket.clientId];
243
- }
256
+ // if (!client.length) {
257
+ // delete clients[socket.clientId];
258
+ // }
244
259
 
245
260
  if (!Object.keys(clients).length) {
246
261
  this.organizations.delete(socket.organization_id);
@@ -265,14 +280,10 @@ class SocketServer extends EventEmitter {
265
280
 
266
281
  if (this.clients.has(socket.clientId)) {
267
282
  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
- }
283
+ delete client[socket.id]
272
284
 
273
- if (!client.length) {
285
+ if (!Object.keys(client).length)
274
286
  this.clients.delete(socket.clientId);
275
- }
276
287
  }
277
288
 
278
289
  if (this.clients.size === 0) {
@@ -296,12 +307,8 @@ class SocketServer extends EventEmitter {
296
307
  if (socket.user_id) {
297
308
  let sockets = this.users.get(socket.user_id)
298
309
  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 {
310
+ delete sockets[socket.id]
311
+ if (!Object.keys(sockets).length) {
305
312
  let userDebounceTimer = sockets
306
313
 
307
314
  clearTimeout(userDebounceTimer);
@@ -495,8 +502,8 @@ class SocketServer extends EventEmitter {
495
502
  }
496
503
 
497
504
  // 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
505
+ if (Data.$filter && Data.$filter.query && Data.$filter.query._id && Data.$filter.query._id.$eq === '$user_id')
506
+ delete Data.$filter.query._id
500
507
 
501
508
 
502
509
  delete Data.socket