Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 GeniusesOfSymfony/WebSocketBundle

I'm working in a symfony 2 application and I need to use websocket.

I found a bundle named GeniusesOfSymfony/WebSocketBundle and I integrate it in the system. This bundle is based on JDare/ClankBundle but the firth has the TopicPeriodicTimerInterface using for resend the information for the client every a defined time.

I have it in my application but I need to get the logged user. The bundle has a service named @gos_web_socket.websocket.client_manipulator to manipulate the user information but when I try to get user information only the service get me the anonymous user but I'm logged for someone user.

Any had the same problem or know a solution for that???

like image 916
Rolando Toledo Fernandez Avatar asked Jul 16 '15 12:07

Rolando Toledo Fernandez


1 Answers

I invite you to follow the procedure for session sharing and user authentication, as explained in the doc of the bundle you use: https://github.com/GeniusesOfSymfony/WebSocketBundle/blob/master/Resources/docs/SessionSetup.md

First you need to implement a Symfony session handler, if you decide to use the PDO Session Handler, the doc is here: http://symfony.com/doc/master/cookbook/configuration/pdo_session_storage.html (Do not forget to create the corresponding DB if you chose so, declaring all the services, parameters, and so on).

Next, you have to set your config.yml to use your session handler:

framework:
    ...
    session:
        handler_id: session.handler.pdo # adapt if you chose a different one

Setup GOS Websocket for using it as well:

gos_web_socket:
    ...
    client:
        firewall: main # the name of your firewall (can be an array if multiple)
        session_handler: @session.handler.pdo

The end of the documentation available at the first link will show you some ways of using your client manipulator. I know it is better to copy paste, but I really don't feel like copy pasting the entire doc is useful, nor clear.

For my own use, I do not have a client manipulator, I simply use

$this->clientStorage->getClient($connection->WAMP->clientStorageId);

in order to retrieve the user with the current connection. The clientStorage is available if you give it (@gos_web_socket.client_storage) to the service constructor as argument. Of course, you need to adapt your constructor:

class AcmeTopic implements TopicInterface
{
    /**
     * @param ClientStorageInterface $clientStorage
     */
    protected $clientStorage;

    public function __construct(ClientStorageInterface $clientStorage)
    {
        ...

To access other users, you can take some inspiration from:

foreach($topic as $subscriber)
{
    $subscriber->event($topic->getId(),
                    ['msg' => $this->clientStorage
                                   ->getClient($connection->WAMP->clientStorageId)
                                   ->getUsername().' is now online!']);
}

Hope this helps, I do not have that much experience with it, as it is my first use as well. I invite you to ask directly on the GitHub if you are still stuck (issues part), as the author can probably help you more!

(Also, I assumed you use the Topic)

like image 116
wr0ng.name Avatar answered Oct 05 '22 03:10

wr0ng.name