How to use @azure/service-bus - 10 common examples

To help you get started, we’ve selected a few @azure/service-bus examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / javascript / gettingStarted / browseMessages.js View on Github external
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();
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / javascript / gettingStarted / receiveMessagesStreaming.js View on Github external
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);
  };
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / typescript / gettingStarted / sendMessages.ts View on Github external
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);
    }
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / javascript / advancedFeatures / deferral.js View on Github external
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);
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / typescript / gettingStarted / receiveMessagesStreaming.ts View on Github external
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();
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / typescript / gettingStarted / session.ts View on Github external
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();
}
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / javascript / gettingStarted / session.js View on Github external
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();
}
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / javascript / gettingStarted / receiveMessagesLoop.js View on Github external
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();
  }
}
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / typescript / gettingStarted / receiveMessagesLoop.ts View on Github external
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();
  }
}
github Azure / azure-sdk-for-js / sdk / servicebus / service-bus / samples / javascript / advancedFeatures / sessionState.js View on Github external
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();
}