Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data from client to redis and then after to laravel

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.

like image 596
Mr. Engineer Avatar asked Oct 02 '17 14:10

Mr. Engineer


People also ask

How does redis integrate with Laravel?

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.

How does redis Cache work Laravel?

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.

What is predis in Laravel?

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.


1 Answers

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/

like image 141
Jacob Thomason Avatar answered Sep 19 '22 11:09

Jacob Thomason