...
 
Commits (2)
......@@ -5,21 +5,40 @@
namespace Minds\Core\Data\PubSub\Redis;
use Minds\Core\Config;
use \Redis as RedisServer;
class Client
{
/**
* @var \Redis
*/
private $redis;
/**
* @var string
*/
private $host;
public function __construct($redis = null)
{
if (class_exists('\Redis')) {
$this->redis = $redis ?: new RedisServer();
$this->redis = $redis ?: new \Redis();
$config = Config::_()->get('redis');
try {
$this->redis->connect($config['pubsub'] ?: $config['master'] ?: '127.0.0.1');
} catch (\Exception $e) {
$this->host = $config['pubsub'] ?: $config['master'] ?: '127.0.0.1';
$this->connect();
}
}
/**
* @throws \Exception
*/
public function connect(): void
{
if (!$this->redis instanceof \Redis) {
$this->redis = new \Redis();
}
if (!$this->redis->isConnected()) {
if (!@$this->redis->connect($this->host)) {
throw new \Exception("Unable to connect to Redis: " . $this->redis->getLastError());
}
}
}
......@@ -34,11 +53,17 @@ class Client
}
}
/**
* @param $channel
* @param string $data
* @return bool|int
*/
public function publish($channel, $data = '')
{
if (!$this->redis) {
if (!$this->redis->isConnected()) {
return false;
}
return $this->redis->publish($channel, $data);
}
}
......@@ -4,15 +4,22 @@
*/
namespace Minds\Core\Sockets;
use Minds\Core\Data\PubSub;
use Minds\Core\Di\Di;
use Minds\Core\Config;
use Minds\Entities\User;
class Events
{
/**
* @var PubSub\Redis\Client
*/
private $redis;
/**
* @var MsgPack
*/
private $msgpack;
private $prefix = 'socket.io';
private $prefix = 'socket.io#';
private $rooms = [];
private $flags = [];
......@@ -25,7 +32,6 @@ class Events
{
$this->redis = $redis ?: Di::_()->get('PubSub\Redis');
$this->msgpack = $msgpack ?: new MsgPack();
$this->prefix = (isset($config['socket-prefix']) ? $config['socket-prefix'] : 'socket.io') . '#';
}
public function emit(/*$event, ...$data*/)
......@@ -82,7 +88,7 @@ class Events
$packed = str_replace(pack('c', 0xdb), pack('c', 0xd9), $packed);
}
// Publish
// Publish - TODO: Log + track possible Redis failures if false is returned
$this->redis->publish($this->prefix . $packet['nsp'] . '#', $packed);
// Reset
......
......@@ -3,6 +3,7 @@
"description": "The core minds social engine",
"require": {
"php": "~7.1",
"ext-redis": "~4.3",
"aws/aws-sdk-php": "^3.38",
"braintree/braintree_php": "3.5.0",
"datastax/php-driver": "1.2.2",
......