I'm using laravel and redis for real time chat. I can fire event from my laravel and receiving that data to client side.
My problem is how can i send something from client and then receive it to redis and pass it to laravel
E.g How can i check if user has read the chat message.
Code :
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require( 'socket.io' ).listen( server );
var redis = require('redis');
var port = process.env.PORT || 8888;
server.listen(port,'x.x.x.x');
io.on('connection', function (socket) {
console.log("Connected");
});
var redisClient = redis.createClient();
redisClient.psubscribe(['get_message','read_message']);
redisClient.on("pmessage", function(channel, pattern, message) {
console.log(channel); // i can see get_message on this line in console but not read_message
});
//Also tried this
io.on('read_message', function (socket) {
console.log(socket);
});
//Also this
redisClient.on('read_message', function (socket) {
console.log(socket);
});
redisClient.on('disconnect', function() {
console.log("Disconnected");
redisClient.quit();
});
Note : I'm emitting data from IOS app.
Redis in Laravel In order to use Redis with Laravel, firstly yo will have to install the predis/predis package. You can install it via composer. Just run this command and you're done. Apart from this, you can also install php redis, a php extension via PECL.
Laravel supports the use of Redis, which uses caches for temporary data storage to speed up the process of performing database queries and getting feedback, which will, in turn, reduce the amount of time spent pulling up data.
predis use pure php code to communicate with redis server.so there is no local php extension requirements. but it's a little slower. so you can deploy you application to any webhost that support php. As I remember. laravel default use predis to make it minial requirements.
Redis is a key/value store engine. You should treat it like a database.
A client sends a request to a webserver that receives this request and decides what to do with it. For requests that require server-side processing, it will pass that request to a server-side "engine" for processing, in your case, probably PHP-FPM, which then passes it to a PHP daemon that will then execute the request.
Redis is not able to interpret a request in this manner. Therefore, you must intercept the request with Laravel and then send it to Redis. Not the other way around.
If you're trying to have Laravel get the information from Redis, you'll want to use Redis' pub/sub feature. Then you can have Laravel subscribe to Redis updates, get the updates and persist or handle the data however you want.
The phpredis lib supports the pub/sub functionality.
Here is an example of the PHP implementation with that lib. https://xmeng.wordpress.com/2011/11/14/pubsub-in-redis-using-php/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With