Skip to content

Add RabbitMQ example for NodeJS#41

Closed
raupie wants to merge 10 commits intoplatformsh:masterfrom
raupie:nodejs-rabbitmq-example
Closed

Add RabbitMQ example for NodeJS#41
raupie wants to merge 10 commits intoplatformsh:masterfrom
raupie:nodejs-rabbitmq-example

Conversation

@raupie
Copy link

@raupie raupie commented Jul 31, 2020

No description provided.

@raupie raupie force-pushed the nodejs-rabbitmq-example branch from 87f5b50 to 62b188b Compare July 31, 2020 19:29
@raupie
Copy link
Author

raupie commented Jul 31, 2020

squashed and rebased

@Crell
Copy link
Contributor

Crell commented Aug 3, 2020

You also need to add RabbitMQ to the list of services in routes.js, toward the top of the file, or it won't get picked up. If you look at the built env you'll see Rabbit is still missing on the Node page.

@raupie raupie force-pushed the nodejs-rabbitmq-example branch from 62b188b to d214ff6 Compare August 19, 2020 13:14
@Crell
Copy link
Contributor

Crell commented Aug 25, 2020

Code looks fine, but stuck build referred to support.

@@ -0,0 +1,30 @@
const amqp = require('amqplib/callback_api');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using the Promise-based API but this imports the callback one.
The Promise-based API is the default one so you can do const amqp = require('amqplib');

let message = 'Deploy Friday!';

await channel.assertQueue(queue, {durable: false});
await channel.sendToQueue(queue, Buffer.from(message))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendToQueue behaves like node's Stream.writable: it returns a boolean, not a Promise. You don't have anything to await here.
You can ignore the returned value though.

JavaScript lets you write anything and try to make sense of it (with all the implications this can have...). That's why nothing complains about the fact that you're awaiting for a primitive value when it would only make sense for a Promise.

await channel.assertQueue(queue, {durable: false});
await channel.sendToQueue(queue, Buffer.from(message))

let output = 'Message sent: ' + queue + ' ' + message;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't mutate any variable so your lets can be consts.
This applies to all the let in the function.


let output = 'Message sent: ' + queue + ' ' + message;

return output;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to consume the message before returning to be consistent with the other language examples.

let output = 'Message sent: ' + queue + ' ' + message;

return output;
} catch (error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is supposed to run just fine in this example, so you might want to get rid of the try/catch just to remove some noise around the code example.

I don't feel strongly about this though.

Just for the sake of completion, async/await is good but not always clearer. It feels like writing synchronous code when things are async so it looks simple and familiar but has some caveats e.g. control flow and error handling. This catch here would catch any error happening in the try block so it makes it hard to have a good error handling mechanism (What broke? The connection? A non-promise-related exception got thrown? Something else?).
It's okay to use a .then(...) approach if that makes things simpler to reason about. Whether or not things are actually "simpler" is pretty subjective though.

@raupie raupie closed this Jan 5, 2021
@raupie raupie deleted the nodejs-rabbitmq-example branch January 5, 2021 20:58
@raupie
Copy link
Author

raupie commented Jan 5, 2021

Been so long, I forget what I was trying to do. Burning down and starting over...

@raupie
Copy link
Author

raupie commented Jan 6, 2021

New PR here #48

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

Successfully merging this pull request may close these issues.

4 participants