I have setup a Ratchet Server to communicate browser <-> server via a websocket. The communication is working, however it seems the Ratchet service is using one Magento/MySQL connection because after ~8 hours (session lifetime) I get an error that MySQL has gone away.

Websocket Communication Script

namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
use Mage;

class Pusher implements WampServerInterface {
 * A lookup of all the topics clients have subscribed to
protected $subscribedTopics = array();

protected $clients;

public function __construct() {
    $this->clients = new \SplObjectStorage;

public function onSubscribe(ConnectionInterface $conn, $topic) {
    $this->subscribedTopics[$topic->getId()] = $topic;

 * @param string JSON'ified string we'll receive from ZeroMQ
public function onBlogEntry($entry) {
    $entryData = json_decode($entry, true);

    // If the lookup topic object isn't set there is no one to publish to
    if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {

    $topic = $this->subscribedTopics[$entryData['category']];

    // re-send the data to all the clients subscribed to that category


public function onUnSubscribe(ConnectionInterface $conn, $topic) {
public function onOpen(ConnectionInterface $conn) {
public function onClose(ConnectionInterface $conn) {
public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
    // In this application if clients send data it's because the user hacked around in console
    $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
    // In this application if clients send data it's because the user hacked around in console

        $helper = Mage::getSingleton('namespace_module/websocket_communication');
        if (strpos($topic, 'request_data') !== false) {
            $helper->requestData($event, $topic);
        } else {

public function onError(ConnectionInterface $conn, \Exception $e) {

My questions are:

  1. Can I use the current session (admin/frontend) to make sure Ratchet gets a current connection each time it needs to "talk" to Magento? How would this work? (We are using Redis for session storage)
  2. If not, how can I "reopen" the connection to Magento to avoid the MySQL gone away error?

I also saw this question which looks extremely similar, but could not figure out how to implement the proposed solutions. Any tips/hints appreciated!

1 Answer 1


At the moment the best solution I have come up with is to run a separate cron that restarts my websocket daemon every 6 hours (so it will catch it before the timeout). If anyone has another solution I would be happy to try it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.