A lightweight and efficient publish-subscribe system in Go, enhanced with optional file logging features.
- Easy-to-use API for publishing and subscribing to topics.
- Built with concurrency in mind to ensure safety across multiple operations.
- Option to log messages to a file for auditing or debugging purposes.
Initialize the PubSub system with:
ps := pubsub.NewPubSub[string]()
It is possible to notice that generics are used, you can choose the type of message
To log messages to a file, activate the file writer. Note that this will panic if there's an issue opening the file:
ps.ActiveFileWriter("path_to_file.txt")
To stop logging and close the file:
ps.CloseFileWriter()
Register for a specific topic and get a channel to receive its messages:
ch := ps.Subscribe("your_topic")
Register a subscriber that uses a handler function to process messages for a specific topic:
unsubscribe := ps.SubscribeWithHandler("your_topic", yourHandlerFunction)
To stop receiving messages from a topic:
ps.Unsubscribe("your_topic", ch)
Send a message to all subscribers of a specific topic. If the file writer is active, the message will also be logged to the file. A panic will occur if there's an error writing to the file:
ps.Publish("your_topic", "your_message")