Categories: Software Development

Accessing Laravel session from Ratchet

Ratchet is an awesome PHP-based websocket server framework. A good choice when you want to add real-time websocket-based features to your PHP-based web application without adding any additional technology. I personally use it on my (personal) MVP Laravel-based web application to keep it as monolithic as possible ?

Since Ratchet has been around for awhile, there are some good tutorials about it. If you just want to have a simple standalone websocket server, the example in its README is enough. To “loosely” integrate Ratchet with Laravel by creating an php artisan command to start the websocket server which can access most Laravel features, this and this articles help a lot. But in my case, I want to access Laravel session despite of driver being used.

After digging answers in StackOverflow, exploring issues in Github and conducting trials and errors. I use code below to access Laravel session (and get authenticated user) in Ratchet (version 0.4.1). I also use Guzzle to handle the PSR-7 request.

namespace App\Http\Controllers;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Illuminate\Session\SessionManager;

class WebSocketController extends Controller implements MessageComponentInterface
{
    protected $clients;

    public function __construct() {
        $this->clients = [];
    }

    private function auth_user($conn)
    {
        $conn->session->start();
        $user_id = $conn->session->get(\Auth::getName());
        return !empty($user_id) ? \App\User::find($user_id) : null;
    }

    public function onOpen(ConnectionInterface $conn)
    {
        // Create a new session handler for this client
        $session = (new SessionManager(\App::getInstance()))->driver();
        // Get the cookies
        $cookies_header = $conn->httpRequest->getHeader('Cookie');
        $cookies = \GuzzleHttp\Psr7\parse_header($cookies_header)[0];
        // Get the laravel's one
        $cookie = urldecode($cookies[\Config::get('session.cookie')]);
        // get the user session id from it
        $session_id = \Crypt::decryptString($cookie);
        // Set the session id to the session handler
        $session->setId($session_id);
        // Bind the session handler to the client connection
        $conn->session = $session;  

        // Get Auth user
        $user = $this->auth_user($conn);
        // echo "{$user->email} connected\n";

        // Store one connection per user
        $this->clients[$user->id] = $conn;
    }

// more code ..

}

Alternatively, you can also use Laravel memcache or database session store driver instead of the file driver (default) so you can utilize Ratchet’s SessionProvider. I haven’t try it myself but it seems quite convenient

0 0 votes
Article Rating
yohanes.gultom@gmail.com

View Comments

Share
Published by
yohanes.gultom@gmail.com
Tags: laravelphp

Recent Posts

Get Unverified SSL Certificate Expiry Date with Python

Getting verified SSL information with Python (3.x) is very easy. Code examples for it are…

3 years ago

Spring Data Couchbase 4 Multibuckets in Spring Boot 2

By default, Spring Data Couchbase implements single-bucket configuration. In this default implementation, all POJO (Plain…

3 years ago

Firebase Auth Emulator with Python

Last year, Google released Firebase Auth Emulator as a new component in Firebase Emulator. In…

4 years ago

Google OIDC token generation/validation

One of the authentication protocol that is supported by most of Google Cloud services is…

4 years ago

Fast geolocation query with PostGIS

If you need to to add a spatial information querying in your application, PostGIS is…

4 years ago

Auto speech-to-text (Indonesian) with AWS Transcribe and Python

Amazon Web Service Transcribe provides API to automatically convert an audio speech file (mp3/wav) into…

5 years ago