RabbitMQ / AMQP: single queue, multiple consumers for same message?

前端 未结 12 2272
不思量自难忘°
不思量自难忘° 2020-11-28 00:53

I am just starting to use RabbitMQ and AMQP in general.

  • I have a queue of messages
  • I have multiple consumers, which I would like to do different thing
12条回答
  •  庸人自扰
    2020-11-28 01:34

    RabbitMQ / AMQP: single queue, multiple consumers for same message and page refresh.

    rabbit.on('ready', function () {    });
        sockjs_chat.on('connection', function (conn) {
    
            conn.on('data', function (message) {
                try {
                    var obj = JSON.parse(message.replace(/\r/g, '').replace(/\n/g, ''));
    
                    if (obj.header == "register") {
    
                        // Connect to RabbitMQ
                        try {
                            conn.exchange = rabbit.exchange(exchange, { type: 'topic',
                                autoDelete: false,
                                durable: false,
                                exclusive: false,
                                confirm: true
                            });
    
                            conn.q = rabbit.queue('my-queue-'+obj.agentID, {
                                durable: false,
                                autoDelete: false,
                                exclusive: false
                            }, function () {
                                conn.channel = 'my-queue-'+obj.agentID;
                                conn.q.bind(conn.exchange, conn.channel);
    
                                conn.q.subscribe(function (message) {
                                    console.log("[MSG] ---> " + JSON.stringify(message));
                                    conn.write(JSON.stringify(message) + "\n");
                                }).addCallback(function(ok) {
                                    ctag[conn.channel] = ok.consumerTag; });
                            });
                        } catch (err) {
                            console.log("Could not create connection to RabbitMQ. \nStack trace -->" + err.stack);
                        }
    
                    } else if (obj.header == "typing") {
    
                        var reply = {
                            type: 'chatMsg',
                            msg: utils.escp(obj.msga),
                            visitorNick: obj.channel,
                            customField1: '',
                            time: utils.getDateTime(),
                            channel: obj.channel
                        };
    
                        conn.exchange.publish('my-queue-'+obj.agentID, reply);
                    }
    
                } catch (err) {
                    console.log("ERROR ----> " + err.stack);
                }
            });
    
            // When the visitor closes or reloads a page we need to unbind from RabbitMQ?
            conn.on('close', function () {
                try {
    
                    // Close the socket
                    conn.close();
    
                    // Close RabbitMQ           
                   conn.q.unsubscribe(ctag[conn.channel]);
    
                } catch (er) {
                    console.log(":::::::: EXCEPTION SOCKJS (ON-CLOSE) ::::::::>>>>>>> " + er.stack);
                }
            });
        });
    

提交回复
热议问题