Documentation ¶
Overview ¶
Package gomongostreams provides a publisher subscriber model for mongo watchstreams. The Subscriber interface can be extended to return a channel, which can be used in graphql subscriptions Refer to this page to know more about mongo change streams -> https://www.mongodb.com/basics/change-streams !Mongo change streams work only on replica servers and not on standalone servers
Index ¶
Constants ¶
const Version = "0.1.0"
Version of the package
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Publisher ¶
type Publisher struct {
// contains filtered or unexported fields
}
Publisher listens to a changestream even generated on a mongodb collection. In order for the publisher to run, it needs a collection object on which it listens and a filter of type mongo.Pipeline which can be used to listen for specific events on the collection.
func (*Publisher) Subscribe ¶
func (p *Publisher) Subscribe(ctx context.Context, subscriber *Subscriber)
Subscribe - publisher will add the @subscriber to its list and notifies on new events by calling OnEvent method of the subscriber The subscriber should implement the Subscriber interface, refer to examples for more information. Remove the subscription by calling cancel() function of the context
type Subscriber ¶
type Subscriber interface {
OnEvent(data interface{}) error
}
Subscriber interface needs to be implemented to subscribe to the publisher. The publisher will call the OnEvent method of the subscriber and provide the data retrived from the mongo change stream.
type SubscriptionManager ¶
type SubscriptionManager struct {
// contains filtered or unexported fields
}
SubscriptionManager holds a map of publishers. It creates a key for the publisher map, which is a combination of the collectionName and filter , which allows reuse of publishers. ! Once instance of Subscription Manager is enough for a Database
func NewSubscriptionManager ¶
func NewSubscriptionManager() *SubscriptionManager
NewSubscriptionManager Creates a new Subscription manager
func (*SubscriptionManager) GetPublisher ¶
func (s *SubscriptionManager) GetPublisher(collection *mongo.Collection, filter mongo.Pipeline) (*Publisher, error)
GetPublisher creates or retrives a Publisher. It creates a key for the publisher, which is a combination of the collectionName and filter, which allows reuse of publishers. If there is publisher matching the key , a new publisher is created
func (*SubscriptionManager) Shutdown ¶
func (s *SubscriptionManager) Shutdown()
Shutdown stops all the publishers.