Git Product home page Git Product logo

gosumer's Introduction

Gosumer

⚡ Improve your application's performance by consuming your Symfony Messenger messages with Go.

✨ Features

  • Consume your messages directly with Go code
  • PostgreSQL support
  • AMQP support
  • Redis support

Installation

Install gosumer with Go

go get github.com/romaixn/gosumer

⚙️ Configuration

PostgreSQL

Add this to your config/packages/messenger.yaml:

framework:
    messenger:
        transports:
            go: # Add this new transport
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                serializer: 'messenger.transport.symfony_serializer' # Required, https://symfony.com/doc/current/messenger.html#serializing-messages
                options:
                    use_notify: true
                    check_delayed_interval: 60000
                    queue_name: go # Required, used to only get right messages in go side
                retry_strategy:
                    max_retries: 3
                    multiplier: 2

Don't forget to specify in the routing part the message to process in Go

RabbitMQ

Create an env variable to create a custom queue (in this example go is the name of the queue):

RABBITMQ_GO_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/go

And use it in config/packages/messenger.yaml:

framework:
    messenger:
        transports:
            go:
                dsn: '%env(RABBITMQ_GO_TRANSPORT_DSN)%'
                serializer: 'messenger.transport.symfony_serializer'
                retry_strategy:
                    max_retries: 3
                    multiplier: 2

Redis

Create an env variable for Redis:

REDIS_TRANSPORT_DSN=redis://localhost:6379/messages

Add the following to your config/packages/messenger.yaml:

framework:
    messenger:
        transports:
            async:
                dsn: "%env(MESSENGER_TRANSPORT_DSN)%"
                options: []

Make sure to specify the message routing in the routing section to process in Go.

Usage

Configure the transport

For PostgreSQL:

database := gosumer.PgDatabase{
    Host:      "localhost",
    Port:      5432,
    User:      "app",
    Password:  "!ChangeMe!",
    Database:  "app",
    TableName: "messenger_messages",
}

If you are using a custom schema, you can specify it with backticks:

database := gosumer.PgDatabase{
Host:      "localhost",
Port:      5432,
User:      "app",
Password:  "!ChangeMe!",
Database:  "app",
TableName: `"myschema"."messenger_messages"`,
}

For RabbitMQ:

database := gosumer.RabbitMQ{
    Host:     "localhost",
    Port:     nil,
    User:     "guest",
    Password: "guest",
    Queue:    "go",
}

For Redis:

database := gosumer.Redis{
    Host:     "localhost",
    Port:     6379,
    User:     "username",
    Password: "password",
    DB:       0,
    Channel:  "channel_name",
}

Listen for messages

Call the Listen

// Define your own structure according to your message
type Message struct {
    ID     int `json:"id"`
    Number int `json:"number"`
}

err := gosumer.Listen(database, process, Message{})

if err != nil {
    log.Fatal(err)
}

With the function to process your messages:

func process(message any, err chan error) {
    log.Printf("Message received: %v", message)

    // No error
    err <- nil

    // if there is an error, used to not delete message if an error occured
    // err <- errors.New("Error occured !")
}

gosumer's People

Contributors

amitkumar-y avatar ashwinkul28 avatar darkweak avatar lemorage avatar romaixn avatar vessaldaneshvar avatar wesnick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gosumer's Issues

Postgres private schema support

By default Symfony prepends the schema name to the pg_notify channel name with a dot if you are using a schema that is not public. Because of the quoting requirements for postgres it does not seem you can use TableName variable interchangeably for channel name. What I found works is

Table Name: "myschema"."messenger_messages"
Channel Name: "myschema.messenger_messages"

I have just added a method func (database PgDatabase) GetChannelName () string that strips quotes and re-wraps in quotes and it is working for me.

Is there a way to get schemas to work without doing this? If not, I could contribute this back with tests and docs if interesting for you.

Add Contributing.md file.

I suggest adding a contributing.md file to the project. You can assign this task to me, and I will create the file

Check new messages every x seconds

Add the capability to listen for all new incoming messages every X seconds, in addition to the existing LISTEN feature (in case LISTEN misses any messages or encounters errors).

Allow the user to customize the structure of the Message

Currently, the process function takes a message parameter of type any. I would like the user to be able to create their own custom structure and use it directly in the process function.

For example, in the example instead of passing message any, passing message Message :

type Message struct {
	ID     int `json:"id"`
	Number int `json:"number"`
}

func processMessage(message Message, err chan error) {
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.