Skip to main content

snippets_js

Integration

New integration from credential
const amqpConnection = await yepcode.integration.amqp('credential-slug')
New integration from plain authentication data
const amqp = require('amqplib');

amqp.connect({
hostname: 'localhost',
username: 'guest',
password: 'guest',
port: '5672',
vhost: '/'
}).then(connection => {
// Your code here
}).catch(console.error);
New integration from plain authentication data with callback API
const amqp = require('amqplib/callback_api');

amqp.connect({
hostname: 'localhost',
username: 'guest',
password: 'guest',
port: '5672',
vhost: '/'
},
(error, connection) => {
if (error) {
console.error(error);
}
// Your code here
}
);

Publisher (Promise)

Publisher (promise)
const queue = "queueName";

const publisher = (connection) => {
return connection
.createChannel()
.then((channel) => {
return channel.assertQueue(queue).then((ok) => {
return channel.sendToQueue(
queue,
Buffer.from("Your message here")
);
});
})
.catch((error) => console.error(error));
};

Publisher (async/await)

Publisher (async/await)
const queue = "queueName";

const publisher = async (connection) => {
try {
const channel = await connection.createChannel();
await channel.assertQueue(queue);
channel.sendToQueue(queue, Buffer.from("Your message here"));
} catch (error) {
console.error(error);
}
};

Publisher (Callback)

Publisher (callback)
const queue = "queueName";

const publisher = (connection) => {
connection.createChannel((error, channel) => {
if (error != null) console.error(error);
channel.assertQueue(queue);
channel.sendToQueue(queue, Buffer.from("Your message here"));
});
};