How To Use Redis As Session Handler For Ratchet Server – Symfony

In order to make use of the full functionality of ratchet you will need to select a session handle in order to be able to send the current user id in the websocket server. Because you will need in the future to send some information to a specific user. To do that you will have to save the connection which is made when you the user connect to the server. For that you will have to use a session handle. 

This tutorial will not cover how to install the redis server, anyway is very easy you will have to install redis and a php extension for redis and that's it.

How to use Redis as session handler

First of all you have to open the framework.yaml file, which is /config/packages and add the following code:

cache:
        app: 'cache.adapter.redis'
        default_redis_provider: "redis://localhost"

and this:

session:
        handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler

The above lines of code will do the work for you, just make sure you don't have handler_id twice declared in this file. Now open up the websocket server file and make the connection to redis in order to pass the information from seession to Pusher. To do that we will need the next piece of code:

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

$session = new RedisSessionHandler($redis);

These are all the steps you need to take in order to make session handle working. Many errors can occur if you don't have all the extension already installed in your system so before move the session handle make sure your redis server is working good.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x