Support the ongoing development of Laravel.io →
Requests Session Packages
Last updated 1 year ago.
0

Hi,

You cannot use Auth::user() anymore with WebSocket. The WebSocket server is handling multiple connections (so Auth::user() dosent have any sense). BUT you can access the user session. One thing you can do is binding the laravel session to the client connection (you will have X session handlers for your X clients). There is the code for Laravel 5 (you must decrypt cookie to get the session id in Laravel 5) :

 public function onOpen(ConnectionInterface $conn) {
    $this->clients->attach($conn);     
    // Create a new session handler for this client
    $session = (new SessionManager(App::getInstance()))->driver();
    // Get the cookies
    $cookies = $conn->WebSocket->request->getCookies();
    // Get the laravel's one
    $laravelCookie = urldecode($cookies[Config::get('session.cookie')]);
    // get the user session id from it
    $idSession = Crypt::decrypt($laravelCookie);
    // Set the session id to the session handler
    $session->setId($idSession);
    // Bind the session handler to the client connection
    $conn->session = $session;
}

public function onMessage(ConnectionInterface $from, $msg) {
    // start the session when the user send a message 
    // (refreshing it to be sure that we have access to the current state of the session)
    $from->session->start();
    // do what you wants with the session 
    // for example you can test if the user is auth and get his id back like this:
    $idUser = $from->session->get(Auth::getName());
    if (!isset($idUser)) {
        echo "the user is not logged via an http session";
    } else {
        $currentUser = User::find($idUser);
    }
    // or you can save data to the session
    $from->session->put('foo', 'bar');
    // ...
    // and at the end. save the session state to the store
    $from->session->save();
}

ps: dont forget the follwing use:

use App;
use Auth;
use Config;
use Crypt;
use App\User;
use Illuminate\Session\SessionManager;

pps: And dont forget you can only access the cookie if the WS server and the Web server are on the same domain.

Last updated 8 years ago.
0

Amazing, thank you Synnvoid!

0

I feel like I am so close but i am getting this error...

PHP Fatal error: Class 'App' not found in /home/justin/calendar/app/Http/Controllers/AppointmentController.php on line 47

[Problem]

$session = (new SessionManager(App::getInstance()))->driver();

[Controller Code]

<?php
namespace App\Http\Controllers;

use App;
use Auth;
use Config;
use Crypt;
use App\User;
use \Ratchet\MessageComponentInterface;
use \Ratchet\ConnectionInterface;
use \App\Thrift\Appointment;
use \FOG\Thrift\Service as ThriftService;
use \RPC;
use \Illuminate\Session\SessionManager;

class AppointmentController extends Controller implements MessageComponentInterface
{
  protected $users;
Last updated 7 years ago.
0

Solved it! Extends Controller was the problem! Should be...

class AppointmentController implements MessageComponentInterface
0

Im also having the class App not found errror, my ratchet class is not extending controller as above.

Im including vendor/autoload.php in the server script

These are my using statements in the ratchett class

use Ratchet\MessageComponentInterface; 
use Ratchet\ConnectionInterface;
use Illuminate\Session\SessionManager;
use SplObjectStorage;
use GuzzleHttp\Client;
use App;
use Auth;
use Config;
use Crypt;
use App\User;

I think the facades need to be registered but not sure how to do this?

Last updated 7 years ago.
0

Ok, I solved it by running the server through an artisan command instead of standalone.

0

Also you need to run rawurldecode on the cookie before passing to crypt::decrypt, otherwise you will get invlid payload exception if the encrypted cookie contains an = sign.

0

Thank you very much SynnVoid!

You helped me a lot and saved me hours!

0

is there an option to do this with laravel 4.2? i seem to have problems with new SessionManager(App::getInstance())->driver();

0

my problem is similar, when I run the server returns the class is not found and I have

use Illuminate \ Http \ Request;

use Ratchet \ MessageComponentInterface;

use Ratchet \ ConnectionInterface;

use Auth;

use App \ Chat;

use App \ Message;

use App \ Profile;

use Illuminate \ Session \ Session;

help

I want to use Auth and ORM for save the msg

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.