Site icon NexGismo

How to create WebSocket Applications using Symfony, Swoole, and Redis

WebSocket Applications using Symfony

WebSocket Applications using Symfony

Fast-paced, real-time interactions are the norm in today’s society in order to enjoy interactive and captivating usage. Live chat, pop-up alerts, and working together are some examples of how customers could work with the clients and update them with what’s new in an instant, thus increasing their satisfaction. Traditional HTTP request-response cycles are not sufficient for these requirements, which is where WebSocket Applications using Symfony come into play.

What are WebSockets?

WebSockets are a protocol that provides full-duplex communication channels over a single long-lived connection between a client and a server. Unlike traditional HTTP requests, which follow a request-response model and are stateless, WebSockets allow for continuous, bidirectional communication. This makes them ideal for applications that require real-time updates.

Benefits of WebSockets

Why Using Swoole and Redis Together with Symfony?

Step 1: Install Symfony

For those who don’t have Symfony, bring into existence a fresh project of Symfony and whatever other requirements needed to make use of composer:

composer create-project symfony/skeleton my_project  
cd my_project

Step 2: Now Install Swoole and Redis

Acquire the Swoole extension through pecl:

pecl install swoole  

In your php.ini file, activate that extension you have just installed:

extension=swoole  

Using composer, install redis as follows:

composer require predis/predis

Step 3: Create the WebSocket Server

Make a fresh subcontract for the code that will run your webSocket server under your Symfony project:

mkdir src/WebSocket

Make a fresh PHP file, ChatWebSocketServer.php and add the following code to configure a WebSocket server on it:

$redis;  <?php  
  
namespace App\WebSocket;  
  
use Swoole\WebSocket\Server;  
use Predis\Client as RedisClient;  
  
class ChatWebSocketServer  
{  
    private $redis;  
  
    public function __construct()  
    {  
        $this->redis = new RedisClient();  
    }  
  
    public function start()  
    {  
        $server = new Server("0.0.0.0", 8080);  
  
        $server->on("start", function (Server $server) {  
            echo "Swoole WebSocket Server started at ws://127.0.0.1:8080\n";  
        });  
  
        $server->on("open", function (Server $server, $request) {  
            echo "Connection opened: {$request->fd}\n";  
            $server->push($request->fd, "Welcome to Swoole WebSocket Server!");  
        });  
  
        $server->on("message", function (Server $server, $frame) {  
            echo "Received message: {$frame->data}\n";  
            $this->redis->publish('chat', $frame->data);  
        });  
  
        $server->on("close", function (Server $server, $fd) {  
            echo "Connection closed: {$fd}\n";  
        });  
  
        $this->redis->subscribe(['chat'], function ($message) use ($server) {  
            foreach ($server->connections as $fd) {  
                $server->push($fd, $message);  
            }  
        });  
  
        $server->start();  
    }  
}

Step 4: Create a Symfony Command to Run the WebSocket Server

Create a new Symfony command to run the WebSocket server. In yoursrc/Command directory, create a file named WebSocketServerCommand.php:

Symfony\Component\Console\Command\Command;  <?php  
  
namespace App\Command;  
  
use App\WebSocket\ChatWebSocketServer;  
use Symfony\Component\Console\Command\Command;  
use Symfony\Component\Console\Input\InputInterface;  
use Symfony\Component\Console\Output\OutputInterface;  
  
class WebSocketServerCommand extends Command  
{  
    protected static $defaultName = 'app:websocket-server';  
  
    protected function configure()  
    {  
        $this->setDescription('Starts the WebSocket server.');  
    }  
  
    protected function execute(InputInterface $input, OutputInterface $output)  
    {  
        $server = new ChatWebSocketServer();  
        $server->start();  
        return Command::SUCCESS;  
    }  
}  

Step 5: Register the Command

You can also register the command in the services.yaml file as follows:

# config/services.yaml  
services:  
    # ...  
    App\Command\WebSocketServerCommand:  
        tags: ['console.command']

Step 6: Create a Client-Side Application

Create an HTML file in the public directory, index.html, to act as a client-side application:

established!");  <!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Symfony WebSocket Chat</title>  
    <style>  
        #chat {  
            width: 300px;  
            height: 400px;  
            border: 1px solid #ccc;  
            overflow-y: scroll;  
            margin-bottom: 10px;  
        }  
    </style>  
</head>  
<body>  
    <div id="chat"></div>  
    <input type="text" id="message" placeholder="Type a message...">  
    <button onclick="sendMessage()">Send</button>  
  
    <script>  
        const conn = new WebSocket('ws://localhost:8080');  
  
        conn.onopen = function(e) {  
            console.log("Connection established!");  
        };  
  
        conn.onmessage = function(e) {  
            const chat = document.getElementById('chat');  
            const message = document.createElement('div');  
            message.textContent = e.data;  
            chat.appendChild(message);  
        };  
  
        function sendMessage() {  
            const input = document.getElementById('message');  
            const msg = input.value;  
            conn.send(msg);  
            input.value = '';  
        }  
    </script>  
</body>  
</html>

Step 7: Run the WebSocket Server

Before you can use the WebSocket server, it must be started, and the following Symfony command will do it:

php bin/console app:websocket-server

Step 8: Open the Client Application

Open the index.html file in a browser. A chat window should have opened, allowing you to chat and send text to other users in real-time.

Scaling and Reliability

In order to make sure your real-time application is scalable and reliable, the following should be taken into account:

Conclusion

Combining Symfony with Swoole and Redis allows anyone to create high-performing real-time WebSocket software applications that can be used in business environments that require scaling. It utilizes the best of every technology in the mix and thus creates a very solid base for responsive and interactive websites. Be it a chat app, real-time notifications, or working together tools, this combination can deal with almost any tasks related to enhancing user experience.

I hope this blog post can be useful for anyone who wishes to know how to manage and create real-time WebSocket applications within the enterprise environment using Symfony, Swoole and Redis. Code on!

References and Further Reading

Other Article to Refer

Exit mobile version