Last active
May 17, 2024 08:00
-
-
Save ryodeushii/5f5be536a0695b2b3bc96701323a2964 to your computer and use it in GitHub Desktop.
rascal example with automatic exchange-queue creation & binding
This file contains 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
import {BrokerAsPromised as Broker, BrokerConfig } from "rascal"; | |
const brokerConfig: BrokerConfig = { | |
vhosts: { | |
'oypoyext': { | |
connection: { | |
url: '<RABBITMQ URL WITH PORT AND CREDENTIALS>', | |
"options": { | |
"heartbeat": 5 | |
}, | |
"socketOptions": { | |
"timeout": 1000 | |
} | |
}, | |
exchanges: { | |
"rascal_created": { | |
assert: true, | |
type: 'headers', | |
options: { | |
durable: true, | |
} | |
} | |
}, | |
queues: { | |
"rascal_created": { | |
assert: true, | |
options: { | |
durable: true, | |
} | |
} | |
}, | |
bindings: { | |
"rascal_created": { | |
source: 'rascal_created', | |
destination: 'rascal_created', | |
bindingKey: 'rascal_created', | |
} | |
}, | |
"publications": { | |
"demo_pub": { | |
"exchange": "rascal_created", | |
"routingKey": "a.b.c" | |
} | |
}, | |
"subscriptions": { | |
"demo_sub": { | |
"queue": "rascal_created" | |
} | |
} | |
} | |
} | |
}; | |
async function bootstrap(): Promise<void> { | |
const broker = await Broker.create(brokerConfig); | |
broker.on('error', console.error); | |
const publication = await broker.publish('demo_pub', 'Hello World!'); | |
publication.on('error', console.error); | |
// Consume a message | |
const subscription = await broker.subscribe('demo_sub'); | |
subscription | |
.on('message', (message, content, ackOrNack) => { | |
console.log(content); | |
ackOrNack(); | |
}) | |
.on('error', console.error); | |
} | |
bootstrap(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment