Socket closed abruptly during opening handshake while trying to connect to RabbitMq

I have a simple NodeJs application that should connect to RabbitMq.

The code:

const amqp = require('amqplib/callback_api');
const amqpUri = "amqp://user:password@localhost:5672"
if (amqpUri == null) throw Error('Missing AMQP_URI environment variable.');
amqp.connect(amqpUri, function(error0, connection) { if (error0) throw error0; connection.createChannel(function(error1, channel) { if (error1) { throw error1; } const exchangeName = 'product.event'; const queueName1 = 'create'; const routingKey1 = 'create'; const queueName2 = 'delete'; const routingKey2 = 'delete'; channel.assertExchange(exchangeName, 'topic', { durable: false, }); // create channel.assertQueue(queueName1, { durable: false, }); channel.bindQueue(queueName1, exchangeName, routingKey1); channel.consume(queueName1, (msg) => consumeCreated(channel, msg)); // delete channel.assertQueue(queueName2, { durable: false, }); channel.bindQueue(queueName2, exchangeName, routingKey2); channel.consume(queueName2, (msg) => consumeDeleted(channel, msg)); });
});

Then run a RabbitMq image with:

docker run -d --hostname my-rabbit --name some-rabbit -p 5672:15672 -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password rabbitmq:3-management

  • I can access the rabbitmq and connect with the credentials user/password at .

For some reason, I have the error:

/home/hamuto/CLO902-Group35/indexer/app.js:12 throw error0; ^
Error: Socket closed abruptly during opening handshake at Socket.endWhileOpening (/home/hamuto/CLO902-Group35/indexer/node_modules/amqplib/lib/connection.js:260:17) at Socket.emit (events.js:326:22) at endReadableNT (_stream_readable.js:1241:12) at processTicksAndRejections (internal/process/task_queues.js:84:21)

2 Answers

i see this is not likely the case as your using the correct port, however I was getting the exact same error messages when I tried using HTTP port instead of tpc port.

Again I know this isnt your issue but if guess it could help you pinpoint potential failpoints.

1

When encountering the "Socket closed abruptly during opening handshake" error while trying to connect to RabbitMQ, it is often due to using the wrong port number. This error typically occurs when the HTTP port (15672) is mistakenly used instead of the TCP port (5672).

To resolve this issue, ensure that you are using the correct TCP port (5672) when establishing the connection to RabbitMQ. By using the correct port, you can avoid the "Socket closed abruptly during the opening handshake" error and successfully connect to RabbitMQ.

Please note that RabbitMQ uses different ports for different protocols. The HTTP port (15672) is typically used for management purposes, while the TCP port (5672) is used for general messaging communication.

Remember to double-check your code or configuration to ensure that the correct port is specified when connecting to RabbitMQ.

AMQP_SERVER=localhost
AMQP_PORT=5672
AMQP_USER=guest
AMQP_PASSWORD=guest

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like