Supercharge your application's performance: Consuming Symfony messenger messages with Go
Romain
Posted on July 21, 2023
Introduction
As applications scale and grow, efficient message processing becomes crucial for maintaining high performance.
While Symfony Messenger excels in dispatching messages, I introduce a tool to supercharge your message consumption process: Gosumer.
Harness the raw power of Go to optimize your application's performance and handle Symfony Messenger messages seamlessly.
In this article, I'll delve into the functionalities of Gosumer and showcase an example of its implementation to demonstrate its capabilities.
Introducing Gosumer
Gosumer is a cutting-edge package designed to enhance your application's performance by efficiently consuming Symfony Messenger messages using Go code.
Built to work seamlessly with PostgreSQL and AMQP, this tool empowers developers to streamline their message processing and unlock unparalleled performance gains.
You can find the source code of this package here; feel free to contribute or share your feedback!
Example: Empowering your application with Gosumer
Let's dive into an illustrative example to showcase Gosumer in action.
Imagine you have a Go application that needs to process incoming messages efficiently.
With Gosumer, you can easily achieve this goal with just a few lines of code:
package main
import (
"encoding/json"
"log"
"github.com/romaixn/gosumer"
)
type Message struct {
ID int `json:"id"`
Number int `json:"number"`
}
func main() {
database := gosumer.PgDatabase{
Host: "localhost",
Port: 5432,
User: "app",
Password: "!ChangeMe!",
Database: "app",
TableName: "messenger_messages",
}
err := gosumer.Listen(database, processMessage, Message{})
if err != nil {
log.Fatal(err)
}
}
func processMessage(message any, err chan error) {
log.Printf("Message received: %v", message)
jsonData, e := json.Marshal(message)
if e != nil {
log.Printf("Error marshalling message: %v", e)
err <- e
return
}
var msg Message
e = json.Unmarshal(jsonData, &msg)
if e != nil {
log.Printf("Error unmarshalling message: %v", e)
err <- e
return
}
log.Printf("Sum of %d and %d is %d", msg.ID, msg.Number, msg.ID+msg.Number)
err <- nil
return
}
In this example, we define a Message struct, representing the data in the incoming messages, it's the same structure that you have defined on the Symfony side (PHP).
Afterwards, we listen to all messages using the gosumer.listen
function and process them through processMessage
.
Once the message is processed and there are no errors, it's deleted from the database.
However, if an error occurs during processing, the message is not deleted and will be re-consumed at a later time.
I already have some ideas for improving the developer experience of the package. I've created a few issues that you can check out here.
Conclusion
Gosumer can help you for optimizing your application's performance by consuming Symfony Messenger messages with Go.
By leveraging the power of Go and Symfony Messenger, your application will be as powerful as a superhero team on an efficiency-boosting, scalability-expanding quest!
Feel free to visit GitHub to explore the project and test it out! I'd love to hear your feedback.
See you on GitHub! ⭐️
Posted on July 21, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
July 21, 2023