Working with session

146 views
Skip to first unread message

Sergio Rinaudo

unread,
Apr 21, 2014, 8:25:11 AM4/21/14
to ratch...@googlegroups.com
Hi Everyone,
I don't know if this is a dumb question or not.

After testing the "hello world" chat application and also the push integration, I started to make some tests using the SessionProvider.
Now the first question is:

1) assuming that my goal is to get from the server a notification when the user session ends (and do what have to be done in the client),
does the
SessionProvider component help with this?

I'm doing my tests on a Zend Framework 2 application. I read in the docs that this component works only with the Symfony custom session handlers,
but

2) what is the reason? Is that the handler must implement the php 5.4 \SessionHandlerInterface ?

Hope somebony could enlight me.

Cheers



cboden

unread,
Apr 21, 2014, 9:35:17 AM4/21/14
to ratch...@googlegroups.com
Hi Sergio,

1) I don't think so. When the connection is made it does a one time fetch of the session data that is kept in memory for the duration of the WebSocket connection. You can not write back to the storage.

2) There were two reasons for this: A custom session handler had to be used in order to access the data as the native php session options weren't accessible and the other reason is that because a Ratchet application handles multiple connections global variables are not scoped to one user so $_SESSION is un-usable; Symfony offered both an OO Session API and multiple storage methods.

I began working on re-writing the Session component on the weekend which will allow for refreshed reads, writes, non-blocking I/O, and a more modular structure with Symfony as an adapter but able to support other session handlers and frameworks. Unfortunately, I don't have a timeline for this component at the moment (it's hard ;)).

Sergio Rinaudo

unread,
Apr 21, 2014, 10:09:10 AM4/21/14
to ratch...@googlegroups.com
Thank you Chris for your reply,
I take this opportunity to ask you if you know if there is any chance using Ratchet to be notified when user session ends and if you have any advices on how do it.

Talking about custom session handler, do you think it is possible to use those brought by Zend Framework 2?

Actually I did a try and at least the server scripts start without errors (and the client connects), this is my server script code (actually a zf2 controller action with I can call from console using zf2 console router )



        // This service will get the same session handler used by the application,
      // it extends the ZF2 cache handler and implements the php 5.4 \SessionHandlerInterface
      $sessionHandler = $this->getServiceLocator()->get('My\Session\SaveHandler\Cache');
       
      // Ratchet app that implements MessageComponentInterface
        $application = new Session();

        $session = new SessionProvider(
            $application,
            $sessionHandler
        );
       
        $server = new App('www.my.com');
        $server->route('/websocket', $session);
        $server->run();



Finally I ask you what is the purpose of the SessionProvider component, I know it is already explained in the doc page ( "the SessionProvider will attach a Symfony2 Session object to each incoming Connection object that will give you read-only access to the session data from your website." ), but
I can't figure out for what I could use it.


Thank you very much for your time!

Cheers


--

---
You received this message because you are subscribed to a topic in the Google Groups "Ratchet" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ratchet-php/ekq5Z9Hxz24/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ratchet-php...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cboden

unread,
May 3, 2014, 1:05:42 PM5/3/14
to ratch...@googlegroups.com
I would like to support ZF2 sessions (and other frameworks) once I have the new session component complete. 

I don't know of a way to automatically know when a session ends. When a session is destroyed you could send a push notification via ZMQ to the Ratchet app letting you know.

This is the Syfmony Session API. So where you would normally do something like this:

$var = $_SESSION['var'];

You would now do this:

$var = $conn->Session->get('var');
To unsubscribe from this group and all its topics, send an email to ratchet-php+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages