@jcbuisson/express-x 1.4.1 → 1.4.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.mjs +21 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -16,7 +16,9 @@ export function expressX(prisma, options = {}) {
16
16
  const services = {}
17
17
 
18
18
  app.connections = {}
19
- let lastConnectionId = 1
19
+ let lastConnectionId = options.initialConnectionId || 1
20
+ // TODO: use redis to store `connections` and `lastConnectionId` for a clustered deployment
21
+ // (a connection/reconnection may occur on two different servers)
20
22
 
21
23
  app.printCnx = (label) => {
22
24
  console.log(label)
@@ -69,7 +71,6 @@ export function expressX(prisma, options = {}) {
69
71
  const beforeMethodHooks = service?.hooks?.before && service.hooks.before[methodName] || []
70
72
  const beforeAllHooks = service?.hooks?.before?.all || []
71
73
  for (const hook of [...beforeMethodHooks, ...beforeAllHooks]) {
72
- // context = await hook(context)
73
74
  await hook(context)
74
75
  }
75
76
 
@@ -82,10 +83,9 @@ export function expressX(prisma, options = {}) {
82
83
  const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
83
84
  const afterAllHooks = service?.hooks?.after?.all || []
84
85
  for (const hook of [...afterMethodHooks, ...afterAllHooks]) {
85
- // context = await hook(context)
86
86
  await hook(context)
87
87
  }
88
- return result
88
+ return context.result
89
89
  }
90
90
 
91
91
  // hooked version of method: `create`, etc., to be called from backend with no context
@@ -258,18 +258,17 @@ export function expressX(prisma, options = {}) {
258
258
  const io = new Server(server)
259
259
 
260
260
  io.on('connection', function(socket) {
261
- app.log('verbose', 'Client connected to the WebSocket')
262
261
  const connection = {
263
262
  id: lastConnectionId++,
263
+ createdAt: new Date(),
264
264
  socket,
265
265
  channelNames: new Set(),
266
- data: {
267
- a: 123
268
- },
266
+ data: {},
269
267
  }
268
+ app.log('verbose', `Client connected ${connection.id}`)
270
269
  // store connection in cache
271
270
  app.connections[connection.id] = connection
272
- app.log('verbose', `Connection ids: ${Object.keys(app.connections)}`)
271
+ // app.log('verbose', `Connection ids: ${Object.keys(app.connections)}`)
273
272
 
274
273
  // emit 'connection' event for app (expressjs extends EventEmitter)
275
274
  app.emit('connection', connection)
@@ -287,6 +286,11 @@ export function expressX(prisma, options = {}) {
287
286
  }, 60 * 1000)
288
287
  })
289
288
 
289
+ socket.on('reconnect', () => {
290
+ console.log('Client reconnected:', socket.id)
291
+ // Add your custom logic to link the reconnected WebSocket with the previous session
292
+ })
293
+
290
294
 
291
295
  // handle connection data transfer caused by a disconnection/reconnection (page reload, network issue, etc.)
292
296
  socket.on('cnx-transfer', async ({ from, to }) => {
@@ -295,16 +299,21 @@ export function expressX(prisma, options = {}) {
295
299
  app.connections[to] = app.connections[from]
296
300
  app.connections[to].socket = socket
297
301
  delete app.connections[from]
298
- // app.printCnx('AFTER TRANSFER')
302
+ app.printCnx('AFTER TRANSFER')
303
+ // send acknowledge to client
304
+ io.emit('cnx-transfer-ack', to)
299
305
  })
300
306
 
307
+ function wait(ms) {
308
+ return new Promise(resolve => setTimeout(resolve, ms));
309
+ }
301
310
 
302
311
  /*
303
312
  * Handle websocket client request
304
313
  * Emit in return a 'client-response' message
305
314
  */
306
315
  socket.on('client-request', async ({ uid, name, action, args }) => {
307
- app.log('verbose', `client-request ${uid} ${name} ${action} ${args}`)
316
+ app.log('verbose', `client-request ${uid} ${name} ${action} ${JSON.stringify(args)}`)
308
317
  if (name in services) {
309
318
  const service = services[name]
310
319
  try {
@@ -313,7 +322,7 @@ export function expressX(prisma, options = {}) {
313
322
  const context = {
314
323
  app,
315
324
  transport: 'ws',
316
- params: { connection, name, action, args },
325
+ params: { connectionId: connection.id, name, action, args },
317
326
  }
318
327
 
319
328
  try {