Last active
June 23, 2024 13:11
-
-
Save bastiW/89e63537abb6b2b12e189b5c8a9341ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const amqp = require('amqplib/callback_api'); | |
const Ably = require('ably') | |
const url = 'amqps://APPID.KEYID:[email protected]/shared' | |
const queue = 'UATwBQ:example-queue' | |
function actionMessage(message) { | |
console.log(message) | |
} | |
function handleError(error) { | |
console.error(error) | |
} | |
amqp.connect(url, (err, conn) => { | |
if (err) { | |
return handleError(err) | |
} | |
/* Opens a channel for communication. The word channel is overloaded | |
and this has nothing to do with pub/sub channels */ | |
conn.createChannel((err, ch) => { | |
if (err) { | |
return handleError(err) | |
} | |
/* Wait for messages published to the Ably Queue */ | |
ch.consume(queue, (item) => { | |
let decodedEnvelope = JSON.parse(item.content) | |
Ably.Realtime.Message.fromEncodedArray(decodedEnvelope.messages).then((messages) => { | |
/* The envelope messages attribute will only contain one message. However, | |
in future versions, we may allow optional bundling of messages into a | |
single queue message and as such this attribute is an Array to support | |
that in future */ | |
messages.forEach((message) => { | |
actionMessage(message) | |
}) | |
/* ACK (success) so that message is removed from queue */ | |
ch.ack(item) | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment