br41nslug / directus-websocket-subscribe

An extension to subscribe to directus updates over a websocket.
MIT License
106 stars 13 forks source link

How would this work over a horizontally scaled Directus app? #2

Closed nodeworks closed 1 year ago

nodeworks commented 2 years ago

Awesome library! iā€™m just wondering how this could effectively be implemented over a horizontally scaled Directus setup? If you have 2 servers running docker swarm with Directus replicated over the two, if an update is made to a collection item through instance A, how would clients connected to instance B receive the update messages?

br41nslug commented 2 years ago

Thanks šŸ˜„ That's a fascinating scenario and to be completely honest i don't know. My first instinct would be simply connect to both but that's hardly practical and scales terribly. Other options like managing a message queue (redis would be my go-to) or creating a maze of webhooks syncing events between instances get real complicated and project specific very quickly.

cliqer commented 1 year ago

@br41nslug can you please implement redis? Right now it is impossible to run directus in a PM2 cluster using your extension.

br41nslug commented 1 year ago

@cliqer That is not something that will be implemented in this extension. But rather something that needs to be implemented in the Directus core to allow horizontal scaling for any custom hook extension/flow.

cliqer commented 1 year ago

@br41nslug Redis support is already included in Directus as a cache exchange mechanism. I use it successfully with a pm2 cluster configuration and KeyDB (a better Redis drop-in).

What I had in mind is an option in the WebSockets extension to enable reading for events through Redis by adding ws-redis or similar and, if an env variable is true, hook ws behind it.

It is a matter of ws not knowing the existence of a fired event in a different process; thus, reading the update through Redis would work.

That would be reasonable, No?

br41nslug commented 1 year ago

With Redis not being required and the current Redis wrapper used by Directus not supporting pub/sub this can be quite tricky. But feel free to give it a shot šŸ˜„