Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main(): Promise {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If receiving from a Subscription, use `createSubscriptionClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
// To receive messages from sessions, use getSessionReceiver instead of getReceiver or look at
// the sample in sessions.ts file
const receiver = queueClient.createReceiver(ReceiveMode.peekLock);
const onMessageHandler: OnMessage = async (brokeredMessage) => {
console.log(`Received message: ${brokeredMessage.body}`);
await brokeredMessage.complete();
};
const onErrorHandler: OnError = (err) => {
console.log("Error occurred: ", err);
};
try {
receiver.registerMessageHandler(onMessageHandler, onErrorHandler, { autoComplete: false });
// Waiting long enough before closing the receiver to receive messages
await delay(5000);
await receiver.close();
async function receiveMessages(ns: ServiceBusClient, sessionId: string): Promise {
// If receiving from a Subscription, use `createSubscriptionClient` instead of `createQueueClient`
const queueClient = ns.createQueueClient(queueName);
const receiver = queueClient.createReceiver(ReceiveMode.peekLock, { sessionId: sessionId });
const onMessage = async (brokeredMessage: ServiceBusMessage) => {
console.log(`Received: ${brokeredMessage.sessionId} - ${brokeredMessage.body} `);
};
const onError: OnError = (err): void => {
console.log(">>>>> Error occurred: ", err);
};
receiver.registerMessageHandler(onMessage, onError);
await delay(5000);
await queueClient.close();
}
async function receiveMessages(sbClient, sessionId) {
// If receiving from a Subscription, use `createSubscriptionClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
const receiver = queueClient.createReceiver(ReceiveMode.peekLock, { sessionId: sessionId });
const onMessage = async (brokeredMessage) => {
console.log(`Received: ${brokeredMessage.sessionId} - ${brokeredMessage.body} `);
};
const onError = (err) => {
console.log(">>>>> Error occurred: ", err);
};
receiver.registerMessageHandler(onMessage, onError);
await delay(5000);
await queueClient.close();
}
async function main() {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If receiving from a Subscription, use `createSubscriptionClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
// To receive messages from sessions, use getSessionReceiver instead of getReceiver or look at
// the sample in sessions.js file
const receiver = queueClient.createReceiver(ReceiveMode.peekLock);
try {
for (let i = 0; i < 10; i++) {
const messages = await receiver.receiveMessages(1, 5);
if (!messages.length) {
console.log("No more messages to receive");
break;
}
console.log(`Received message #${i}: ${messages[0].body}`);
await messages[0].complete();
}
await queueClient.close();
} finally {
await sbClient.close();
}
}
async function main(): Promise {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If receiving from a Subscription, use `createSubscriptionClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
// To receive messages from sessions, use getSessionReceiver instead of getReceiver or look at
// the sample in sessions.ts file
const receiver = queueClient.createReceiver(ReceiveMode.peekLock);
try {
for (let i = 0; i < 10; i++) {
const messages = await receiver.receiveMessages(1, 5);
if (!messages.length) {
console.log("No more messages to receive");
break;
}
console.log(`Received message #${i}: ${messages[0].body}`);
await messages[0].complete();
}
await queueClient.close();
} finally {
await sbClient.close();
}
}
async function getSessionState(sessionId) {
// If receiving from a Subscription, use `createSubscriptionClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(userEventsQueueName);
const sessionReceiver = queueClient.createReceiver(ReceiveMode.peekLock, {
sessionId: sessionId
});
const sessionState = await sessionReceiver.getState();
if (sessionState) {
// Get list of items
console.log(`\nItems in cart for ${sessionId}: ${sessionState}\n`);
} else {
console.log(`\nNo Items were added to cart for ${sessionId}\n`);
}
await sessionReceiver.close();
await queueClient.close();
}
await brokeredMessage.defer();
}
} else {
// we dead-letter the message if we don't know what to do with it.
console.log(
"Unknown message received, moving it to dead-letter queue ",
brokeredMessage.body
);
await brokeredMessage.deadLetter();
}
};
const onError: OnError = (err) => {
console.log(">>>>> Error occurred: ", err);
};
let receiver = queueClient.createReceiver(ReceiveMode.peekLock);
receiver.registerMessageHandler(onMessage, onError, { autoComplete: false }); // Disabling autoComplete so we can control when message can be completed, deferred or deadlettered
await delay(10000);
await receiver.close();
console.log("Total number of deferred messages:", deferredSteps.size);
receiver = queueClient.createReceiver(ReceiveMode.peekLock);
// Now we process the deferred messages
while (deferredSteps.size > 0) {
const step = lastProcessedRecipeStep + 1;
const sequenceNumber = deferredSteps.get(step);
const message = await receiver.receiveDeferredMessage(sequenceNumber);
if (message) {
console.log("Process deferred message:", message.body);
await message.complete();
} else {
console.log("No message found for step number ", step);
async function receiveMessages(sbClient) {
const subscription1 = sbClient
.createSubscriptionClient(topicName, subscriptionName1)
.createReceiver(ReceiveMode.peekLock);
const subscription2 = sbClient
.createSubscriptionClient(topicName, subscriptionName2)
.createReceiver(ReceiveMode.peekLock);
const subscription3 = sbClient
.createSubscriptionClient(topicName, subscriptionName3)
.createReceiver(ReceiveMode.peekLock);
const messagesFromSubscription1 = await subscription1.receiveMessages(10, 5);
console.log(">>>>> Messages from the first subscription:");
for (let i = 0; i < messagesFromSubscription1.length; i++) {
console.log(messagesFromSubscription1[i].body);
await messagesFromSubscription1[i].complete();
}
await subscription1.close();
const messagesFromSubscription2 = await subscription2.receiveMessages(10, 5);
async function receiveMessages(sbClient) {
const subscription1 = sbClient
.createSubscriptionClient(topicName, subscriptionName1)
.createReceiver(ReceiveMode.peekLock);
const subscription2 = sbClient
.createSubscriptionClient(topicName, subscriptionName2)
.createReceiver(ReceiveMode.peekLock);
const subscription3 = sbClient
.createSubscriptionClient(topicName, subscriptionName3)
.createReceiver(ReceiveMode.peekLock);
const messagesFromSubscription1 = await subscription1.receiveMessages(10, 5);
console.log(">>>>> Messages from the first subscription:");
for (let i = 0; i < messagesFromSubscription1.length; i++) {
console.log(messagesFromSubscription1[i].body);
await messagesFromSubscription1[i].complete();
}
await subscription1.close();
const messagesFromSubscription2 = await subscription2.receiveMessages(10, 5);
console.log(">>>>> Messages from the second subscription:");
for (let i = 0; i < messagesFromSubscription2.length; i++) {
console.log(messagesFromSubscription2[i].body);
await messagesFromSubscription2[i].complete();
}
await subscription2.close();