Skip to content

Channels closed from server do not reconnect #347

Open
@anzerr

Description

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;
    }
});

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions