@jcbuisson/express-x 1.4.2 → 1.4.4

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 +12 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -17,6 +17,8 @@ export function expressX(prisma, options = {}) {
17
17
 
18
18
  app.connections = {}
19
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)
@@ -256,18 +258,17 @@ export function expressX(prisma, options = {}) {
256
258
  const io = new Server(server)
257
259
 
258
260
  io.on('connection', function(socket) {
259
- app.log('verbose', 'Client connected to the WebSocket')
260
261
  const connection = {
261
262
  id: lastConnectionId++,
263
+ createdAt: new Date(),
262
264
  socket,
263
265
  channelNames: new Set(),
264
- data: {
265
- a: 123
266
- },
266
+ data: {},
267
267
  }
268
+ app.log('verbose', `Client connected ${connection.id}`)
268
269
  // store connection in cache
269
270
  app.connections[connection.id] = connection
270
- app.log('verbose', `Connection ids: ${Object.keys(app.connections)}`)
271
+ // app.log('verbose', `Connection ids: ${Object.keys(app.connections)}`)
271
272
 
272
273
  // emit 'connection' event for app (expressjs extends EventEmitter)
273
274
  app.emit('connection', connection)
@@ -282,7 +283,7 @@ export function expressX(prisma, options = {}) {
282
283
  setTimeout(() => {
283
284
  app.log('verbose', `Delete connection ${connection.id}`)
284
285
  delete app.connections[connection.id]
285
- }, 60 * 1000)
286
+ }, 10 * 1000)
286
287
  })
287
288
 
288
289
 
@@ -293,16 +294,17 @@ export function expressX(prisma, options = {}) {
293
294
  app.connections[to] = app.connections[from]
294
295
  app.connections[to].socket = socket
295
296
  delete app.connections[from]
296
- // app.printCnx('AFTER TRANSFER')
297
+ app.printCnx('AFTER TRANSFER')
298
+ // send acknowledge to client
299
+ io.emit('cnx-transfer-ack', to)
297
300
  })
298
301
 
299
-
300
302
  /*
301
303
  * Handle websocket client request
302
304
  * Emit in return a 'client-response' message
303
305
  */
304
306
  socket.on('client-request', async ({ uid, name, action, args }) => {
305
- app.log('verbose', `client-request ${uid} ${name} ${action} ${args}`)
307
+ app.log('verbose', `client-request ${uid} ${name} ${action} ${JSON.stringify(args)}`)
306
308
  if (name in services) {
307
309
  const service = services[name]
308
310
  try {
@@ -311,7 +313,7 @@ export function expressX(prisma, options = {}) {
311
313
  const context = {
312
314
  app,
313
315
  transport: 'ws',
314
- params: { connection, name, action, args },
316
+ params: { connectionId: connection.id, name, action, args },
315
317
  }
316
318
 
317
319
  try {