Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channels closed from server do not reconnect #347

Open
anzerr opened this issue Aug 17, 2023 · 0 comments
Open

Channels closed from server do not reconnect #347

anzerr opened this issue Aug 17, 2023 · 0 comments

Comments

@anzerr
Copy link

anzerr commented Aug 17, 2023

I ran into this problem where I'd do and action that caused the server to close the channel Example: publishing to a missing exchange. I expected the channel to be recreated but I get stuck with a closed/broken channel. If I cause a disconnection from Rabbitmq the channel gets recreated and everything works again.

Is this the expected behavior? Is there a better way of handling this case?

Here's an example with the workaround I'm using for the moment.

const amqp = require('amqp-connection-manager');

const client = amqp.connect('amqp://admin:admin@localhost:5672');
const channelWrapper = client.createChannel({
    setup: (channel) => {
        console.log('setup channel');

        // add this hack to reconnect if the server closes the connection (workaround)
        channel.on('close', () => {
            if (channel.sendMessage === channel.sendOrEnqueue) {
                channelWrapper._onConnect({connection: channelWrapper._connectionManager.connection});
            }
        });

        setTimeout(async () => {
            try {
                await channel.publish('missing_exchange', '', Buffer.from('msg'));
            } catch (err) {
                console.log('published to wrong exchange server closes channel', err);
            }
        }, 1000);
        return channel;
    }
});
@anzerr anzerr changed the title channels closed from server do not reconnect Channels closed from server do not reconnect Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant