简体   繁体   中英

Best way implementing messaging queue

I want to implement some kind of messaging queue system in our webproject. I'm just wondering what's the best way to do this. It's written in PHP (Yii Framework) with MySQL as the database system.

The idea:

  • User 1 sends a message to User 2, but the message will be placed in the future (ie 02-11-2013 14:31)
  • User 2 receives the message from User 1 in his/her inbox at 02-11-2013 14:31 AND receives an notification email about this new message.

I thought about 2 ways to do this:

  1. Insert the message from User 1 in a 'message' table with a date and time the message should be delivered to User 2. User 2 can only read it when this date and time are smaller than the current date and time ( WHERE addTime <= NOW() ).

  2. Insert the message from User 1 in a 'messageQueue' table. This is a table that contains all the messages that needs to be placed in the future. A cronjob (which will run every minute or so) will place all the messages with a date and time smaller than the current time into the 'message' table and deletes it from the 'messageQueue' table. An e-mail can be send directly to User 2.

Cons:

  1. Method 1: no way to send a notification email to User 2
  2. Method 2: you will have to set a cronjob that runs every minute (a message can be placed in the future with a precision of one minute). The notification e-mail can be send directly, but it will be a heavy system with a lot of messages. If the cronjob takes longer than a minute it will be overlapping each other, which can result in a server shutdown eventually.

Does anyone have any idea to do this? I'm actually looking for some push function from the server/database, which tells my script that a new message is delivered to User 2 and that an e-mail needs to be send to User 2.

You can use CURL instead of CRON which will self request your "CRON JOB SCRIPT". It will be called after previous is finished. You might wanna delay CURL request if script will run under 1 minute or so.

Please consider using RabbitMQ implementing the AMQP model ( http://www.rabbitmq.com/ ). It might be a bit "overkill" given the context of the application, you've exposed.

However, if you already know for sure this messaging system would necessarily need to grow quickly, you might want to have a look at the documentation of php-amqplib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM