Codigo doc evo

mail@pastecode.io avatar
unknown
javascript
2 months ago
952 B
1
Indexable
Never
import { Connection, Channel, connect, Message } from 'amqplib';
import RabbitmqServer from '../rabbitmq-server';

router.post('/express', async function(req, res, next){
  const server = new RabbitmqServer('amqp://admin:admin@rabbitmq:5672');
  const server.start();
  res.send(req.body);
});

export default class RabbitmqServer {
  private conn: Connection;
  private channel: Channel;
}

async start(): Promise<void> {
  this.conn = await connect (this.uri)
  this.channel = await this.conn.createChannel;
}

async consume(queue: string, callback: (Message) => void) {
    return this.channel.consume (queue, (message) => {
      callback(message);
      // Acknowledge to rabbit that the queue was consumed
      this.channel.ack(message);
  })
} 

let instanceName = 'instance'

// Calls the consume above
consume(instanceName, (message) => console.log(
  // Log the message
  message.content.toString()
));

Leave a Comment