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() {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If using Topics & Subscription, use createSubscriptionClient to peek from the subscription
const queueClient = sbClient.createQueueClient(queueName);
try {
for (let i = 0; i < 20; i++) {
const messages = await queueClient.peek();
if (!messages.length) {
console.log("No more messages to peek");
break;
}
console.log(`Peeking message #${i}: ${messages[0].body}`);
}
await queueClient.close();
} finally {
await sbClient.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);
const onMessageHandler = async (brokeredMessage) => {
console.log(`Received message: ${brokeredMessage.body}`);
await brokeredMessage.complete();
};
const onErrorHandler = (err) => {
console.log("Error occurred: ", err);
};
async function main(): Promise {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If sending to a Topic, use `createTopicClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
const sender = queueClient.createSender();
try {
for (let index = 0; index < listOfScientists.length; index++) {
const scientist = listOfScientists[index];
const message: SendableMessageInfo = {
body: `${scientist.firstName} ${scientist.name}`,
label: "Scientist"
};
console.log(`Sending message: ${message.body} - ${message.label}`);
await sender.send(message);
}
async function receiveMessage() {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If receiving from a Subscription, use `createSubscriptionClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
const deferredSteps = new Map();
let lastProcessedRecipeStep = 0;
try {
const onMessage = async (brokeredMessage) => {
if (
brokeredMessage.label === "RecipeStep" &&
brokeredMessage.contentType === "application/json"
) {
const message = brokeredMessage.body;
// now let's check whether the step we received is the step we expect at this stage of the workflow
if (message.step === lastProcessedRecipeStep + 1) {
console.log("Process received message:", message);
async function sendMessages() {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If sending to a Topic, use `createTopicClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
const sender = queueClient.createSender();
const data = [
{ step: 1, title: "Shop" },
{ step: 2, title: "Unpack" },
{ step: 3, title: "Prepare" },
{ step: 4, title: "Cook" },
{ step: 5, title: "Eat" }
];
const promises = new Array();
for (let index = 0; index < data.length; index++) {
const message = {
body: data[index],
label: "RecipeStep",
Alice adds 3 items to the shopping cart and checks out, whereas Bob adds 3 items and leaves without
checking out to likely return later.
The session state keeps track of the cart items accordingly.
Setup: To run this sample, you would need session enabled Queue/Subscription.
See https://docs.microsoft.com/en-us/azure/service-bus-messaging/message-sessions#message-session-state
to learn about session state.
*/
const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus");
// Define connection string and related Service Bus entity names here
const connectionString = "";
const userEventsQueueName = "";
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
async function main() {
try {
await runScenario();
} finally {
await sbClient.close();
}
}
async function runScenario() {
// User activity data for Alice and Bob
const shoppingEventsDataAlice = [
{ event_name: "Add Item", event_details: "Milk" },
{ event_name: "Add Item", event_details: "Bread" },
{ event_name: "Add Item", event_details: "Eggs" },
{ event_name: "Checkout", event_details: "Success" }
This sample demonstrates retrieving a message from a dead letter queue, editing it and
sending it back to the main queue.
Prior to running this sample, run the sample in movingMessagesToDLQ.ts file to move a message
to the Dead Letter Queue
*/
import { ServiceBusMessage, ServiceBusClient, ReceiveMode, QueueClient } from "@azure/service-bus";
// Define connection string and related Service Bus entity names here
const connectionString = "";
const queueName = "";
// If deadlettered messages are from Subscription, use `TopicClient.getDeadLetterTopicPath` instead
const deadLetterQueueName = QueueClient.getDeadLetterQueuePath(queueName);
const sbClient: ServiceBusClient = ServiceBusClient.createFromConnectionString(connectionString);
async function main(): Promise {
try {
await processDeadletterMessageQueue();
} finally {
await sbClient.close();
}
}
async function processDeadletterMessageQueue(): Promise {
const queueClient = sbClient.createQueueClient(deadLetterQueueName);
const receiver = queueClient.createReceiver(ReceiveMode.peekLock);
const messages = await receiver.receiveMessages(1);
if (messages.length > 0) {
async function sendMessages(): Promise {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If sending to a Topic, use `createTopicClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
const sender = queueClient.createSender();
const data = [
{ step: 1, title: "Shop" },
{ step: 2, title: "Unpack" },
{ step: 3, title: "Prepare" },
{ step: 4, title: "Cook" },
{ step: 5, title: "Eat" }
];
const promises = new Array();
for (let index = 0; index < data.length; index++) {
const message = {
body: data[index],
label: "RecipeStep",
async function main(): Promise {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
try {
await sendScheduledMessages(sbClient);
await receiveMessages(sbClient);
} finally {
await sbClient.close();
}
}
async function main() {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
try {
await sendScheduledMessages(sbClient);
await receiveMessages(sbClient);
} finally {
await sbClient.close();
}
}