Make Use of PHPREDIS in Laravel App As Replacement For the Default PREDIS... Which Would Work The Same Configuration as PREDIS...
add this at composer.json
"nardev/laravel-5.1-phpredis": "dev-master"
then composer update
In config/app.php
Uncomment Redis Service Provider
// Illuminate\Redis\RedisServiceProvider::class,
Uncomment Redis Facade
// 'Redis' => Illuminate\Support\Facades\Redis::class,
Add this as a Replacement for the Uncommented Lines
Nardev\PhpRedis\PhpRedisServiceProvider::class,
'PHPRedis' => Illuminate\Support\Facades\Redis::class,
Now to Test If it works
Route::get('message', function () {
$app = PHPRedis::connection();
$app->set("masterpowers", "Yeah Baby Yeah");
print_r($app->get("masterpowers"));
});
You Should Get Result of
Yeah Baby Yeah
To Test Redis PubSub Using PHPREDIS Facade Add this to Your Root Folder and name it socket.js
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
app.listen(6001, function() {
console.log('Server is running!');
});
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {});
redis.psubscribe('*', function(err, count) {});
redis.on('pmessage', function(subscribed, channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
Now in Your Routes File
Route::get('sender', function () {
$data = [
'event' => 'UserSignedUp',
'data' => [
'username' => 'JohnDoe',
],
];
PHPRedis::publish('test-chanel', json_encode($data));
return "User Signed Up!";
});
Route::get('receiver', function () {
return view('receiver');
});
Then Simply Run node socket.js
And For Redis you can type redis-cli --port 6001
Then In Your View if Your Using Vue Js You Can Do this
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h1>New Users</h1>
<ul>
<li v-repeat="user: users">@{{ user }}</li>
</ul>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.16/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js"></script>
<script>
var socket = io(window.location.origin + ':6001');
new Vue({
el: 'body',
data: {
users: []
},
ready: function() {
socket.on('test-chanel:UserSignedUp', function(data) {
this.users.push(data.username);
}.bind(this));
}
});
</script>
</body>
</html>
If You Visit /receiver Route You Should See Name "John Doe" is Added in the List Everytime You Hit /sender
I Also Successfully Implement Redis Pub/Sub RealTime Broadcasting Using this Tutorial Inside Laracast!
https://laracasts.com/discuss/channels/general-discussion/step-by-step-guide-to-installing-socketio-and-broadcasting-events-with-laravel-51/
The Only Difference is Im Using PHPREDIS Behind the Scene and Not Predis
Hope Someone Find This Thread useful! Comments and Suggestions Are Welcome
:D