Lin Du
Lin Du

Reputation: 102307

Should I create message channel at runtime?

When using MQ system, like RabbitMQ, Google Pub/Sub.

Should I create a message channel/queue at runtime of the application? Or, Create it manually first?

For example, when using Google Pub/Sub, create topic at runtime.

async function createTopic(topicName: string): Promise<any> {
  const topicInstance = pubsubClient.topic(topicName);
  const [exists] = await topicInstance.exists();
  if (exists) {
    logger.info(`${topicName} topic is existed`);
    return;
  }
  return pubsubClient
    .createTopic(topicName)
    .then((data) => {
      logger.info(`Create topic ${topicName} successfully`);
      return data;
    })
    .catch((err) => logger.error(err));
}

Especially considering development, deployment, and continuous integration processes.

I read from a book that creating a message queue in real time is not very useful.

Upvotes: 0

Views: 60

Answers (1)

Daniel Collins
Daniel Collins

Reputation: 812

There is nothing that prevents you from creating the topic at runtime. However, unless you have clients which are checking for the topic's existence and waiting to subscribe to it, you would be publishing messages that will never be received. A better pattern would be to establish your topic beforehand with autoscaling subscribers (perhaps running in cloud functions) ready to receive messages and take appropriate action whenever your publisher begins generating them.

Upvotes: 1

Related Questions