Skip to content

Latest commit

 

History

History
37 lines (28 loc) · 1.3 KB

producing-messages.md

File metadata and controls

37 lines (28 loc) · 1.3 KB

RedisSMQ / Docs / Producing Messages

Producing Messages

A Producer instance allows to publish a message to a queue.

You can use a single Producer instance to produce messages, including messages with priority, to one or multiple queues.

Before publishing a message do not forget to set an exchange for the message using ProducibleMessage.setQueue(), ProducibleMessage.setTopic(), or ProducibleMessage.setFanOut(). Otherwise, an error will be returned.

'use strict';
const { ProducibleMessage, Producer } = require('redis-smq');

const producer = new Producer();

// Always run the producer before producing a message
producer.run((err) => {
  if (err) console.error(err);
  else {
    const msg = new ProducibleMessage();
    msg
      .setBody({ hello: 'world' })
      .setTTL(3600000) // message expiration (in millis)
      .setQueue('test_queue'); // setting up a direct exchange
    producer.produce(msg, (err, reply) => {
      if (err) console.error(err);
      else console.log('Successfully produced');
    });
  }
});

For more details see: