adonisjs / transmit

A Server-Sent-Event module for AdonisJS
MIT License
65 stars 4 forks source link

Transmit doesn't work with PM2 cluster #19

Closed sivo1981 closed 3 months ago

sivo1981 commented 4 months ago

Package version

@adonisjs/transmit@1.0.0

Describe the bug

When application is deployed to PM2 cluster, subscribe request fails randomly on some nodes, because this line returns false.

https://github.com/adonisjs/transmit/blob/68f41f2bae4625755e5fe891c97fed2b4673c126/src/transmit.ts#L198

I have configured Redis with ioredis and this works fine for subscribeToChannel, but the problem is that createStream doesn't publish UID to all other nodes with this.#bus?.publish and StreamChannelRepository has an empty set.

https://github.com/adonisjs/transmit/blob/68f41f2bae4625755e5fe891c97fed2b4673c126/src/stream_channel_repository.ts#L28

Reproduction repo

No response

RomainLanz commented 4 months ago

Hey @sivo1981! 👋🏻

problem is that createStream doesn't publish UID to all other nodes

This is the expected behavior. Your registration stays within one Node (where the stream was open).

Could you give me more information on what does not work? The best would be to create a small reproduction or a failing test.

sivo1981 commented 4 months ago

Application is started on PM2 with 2 instances.

Transmit config:

export default defineConfig({
  pingInterval: '1m',
  transport: {
    driver: redis({
      host: env.get('REDIS_HOST'),
      port: env.get('REDIS_PORT'),
      password: env.get('REDIS_PASSWORD')
    })
  }
})

PM2 logs:

NODE-1 | Transmit connected: 55f07e6f-0562-4b93-949f-ff2bf139ab3d"}
NODE-1 | Transmit subscribed 55f07e6f-0562-4b93-949f-ff2bf139ab3d to users
NODE-1 | POST - /__transmit/subscribe - 204 - 19 ms
NODE-0 | Transmit subscribed 55f07e6f-0562-4b93-949f-ff2bf139ab3d to users/21
NODE-0 | POST - /__transmit/subscribe - 400 - 9.35 ms

Initial transmit connect was made on node 1 and first subscribe is successful beacuse it was requeste on the same node as connect. The problem is in second subscribe request which goes on node 0 and response with HTTP 400 code. This happens because this.#storage.addChannelToStream(uid, channel) returns false.

Hope this helps you investigate this problem.

sivo1981 commented 4 months ago

Any news regarding this issue?

RomainLanz commented 4 months ago

Yes, sorry for the late reply. The status code sent back is wrong and will be fixed shortly, but the subscription should still be broadcasted and registered despite the response sent by the backend.

Have you tried it or stopped when getting this 400?

sivo1981 commented 4 months ago

I can confirm that despite getting HTTP 400 error code, subscription is successful on the server and events are received on the client normally.

RomainLanz commented 4 months ago

Perfect thanks!

RomainLanz commented 3 months ago

Released in 1.0.1