ThreeDotsLabs / watermill

Building event-driven applications the easy way in Go.
https://watermill.io
MIT License
7.71k stars 406 forks source link

example: server-sent-events-htmx: How to swap the PUB SUB system ? #455

Closed gedw99 closed 3 months ago

gedw99 commented 4 months ago

https://github.com/ThreeDotsLabs/watermill/tree/master/_examples/real-world-examples/

How can I swap this out to use NATS Jetstream ?

My understanding is that it's pretty easy to swap out to NATS or even a DB with Watermill ?

gedw99 commented 4 months ago

I made all the code changes to support both Google Pub Sub and Nats Jetstream as a PR.

But it looks like the Interface is not the same for each :

"github.com/ThreeDotsLabs/watermill-googlecloud/pkg/googlecloud" "github.com/ThreeDotsLabs/watermill-nats/v2/pkg/nats"

Any thoughts / advice ?

m110 commented 4 months ago

Hey @gedw99, can you share the code you changed?

You need to replace the googlecloud.NewPublisher and googlecloud.NewSubscriber calls with the equivalent constructors of nats. The returned values always implement the Publisher and Subscriber interfaces.

gedw99 commented 4 months ago

Hey @m110

thanks for the follow up. I have to come back on this, as deep in the weeds on something else right now, but I wanted to ask in the mean time :

Could we do a Factory pattern ( the golang way though ), on this code such that a config can drive which one is loaded ? Or would a compile time go build tag be better ?

My intent here is to make it easy to not have to do this again and again in Watermill....

m110 commented 4 months ago

Yes, sure. You could use a factory with a method that returns the message.Publisher or the message.Subscriber, and it would decide what implementation to use. :)

gedw99 commented 4 months ago

Ok thanks @m110

I am snowed under, so if someone else wants to take this go for it. Otherwise I will swing back on to this.

The solution seems to be worked out.