Current Location: Home> Latest Articles> Complete Guide to Implementing Real-Time Redis Message Subscription and Email Notifications in PHP

Complete Guide to Implementing Real-Time Redis Message Subscription and Email Notifications in PHP

M66 2025-10-28

Overview: Implementing Redis Message Subscription and Email Notifications in PHP

In modern real-time applications, message notifications are an essential component. Redis provides a publish/subscribe mechanism that enables instant communication between servers and clients. By combining PHP with the Swoole coroutine extension, you can build a high-performance system that continuously listens for Redis messages and triggers email notifications efficiently.

Installing Required Dependencies

Before writing any code, make sure that both the Swoole and Redis extensions are installed in your environment. On Linux systems, you can install them using the following commands:

pecl install swoole
pecl install redis

If you're using Windows, download the appropriate DLL extension files from the official PECL website and install them manually.

Creating the PHP Listener Script

Create a file named notify.php to continuously listen to Redis message subscriptions and trigger email notifications when new messages arrive. Example code:

<?php
require 'vendor/autoload.php';
use Swoole\Coroutine;
use Swoole\Coroutine\Redis;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$redis->subscribe(['channel'], function ($redis, $channel, $message) {
    // Send email notification when message is received
    sendEmail($message);
});

Coroutine::create(function () use ($redis) {
    $redis->subscribe(['channel'], function ($redis, $channel, $message) {
        // Send email notification when message is received
        sendEmail($message);
    });
});

function sendEmail($message) {
    // Use your preferred mail library to send the email
    // code...
}

Coroutine::create(function () use ($redis) {
    while (true) {
        $redis->ping();
        Coroutine::sleep(5);
    }
});

In this code, we first instantiate a Redis client and connect it to the Redis server. The subscribe method listens to one or more channels. When a message is received, the callback function is triggered, and the sendEmail() function is called to send the email notification. You can implement this using libraries such as PHPMailer or SwiftMailer.

Additionally, the use of Swoole coroutines allows the program to stay alive and periodically ping the Redis server to maintain a persistent connection.

Running the Listener Script

After saving the script, run the following command in your terminal to start listening:

php notify.php

The script will continue running, listening for new messages from the subscribed Redis channel, and automatically send email notifications when a message is received.

Conclusion

By combining PHP's Swoole coroutine extension with Redis's publish/subscribe mechanism, developers can efficiently implement real-time message listening and email notification systems. This architecture is ideal for real-time chat platforms, alert systems, and data update push services. It simplifies asynchronous workflows and significantly improves performance and response speed in real-time applications.