How to send data from client to redis and then after to laravel
Asked Answered
G

3

14

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.

Gadget answered 2/10, 2017 at 14:16 Comment(3)
You should be using AJAX requests for asynchronous client to server updatesNikaniki
Did you read my note at the last?Gadget
Yes I did, why would you be unable to send requests to your server from your client?Nikaniki
B
5

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/

Bissell answered 6/10, 2017 at 7:17 Comment(0)
M
2

You can leverage Broadcasting in Laravel for the same. Also you can broadcast between multiple clients using whisper. Also there is a redis based broadcaster in it.

First you need to include its package in composer.

composer require pusher/pusher-php-server "~3.0"

Configure the credentials in app/broadcasting.php

'options' => [
'cluster' => 'eu',
'encrypted' => true
],

You need to configure broadcasting queue as well.

Sending Broadcast

event(new ShippingStatusUpdated($update)); //Broadcasts to everyone, avail to working scope as well
broadcast(new ShippingStatusUpdated($update));//Broadcasts only to others

Receiving Broadcast

Instantiate socket ID for echo [included in header as X-Socket-ID]

var socketId = Echo.socketId();

Make sure you have echo installed

npm install --save laravel-echo pusher-js

Create echo instance

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your-pusher-key'
});

Listening events

Echo.channel('orders')
    .listen('OrderShipped', (e) => {
        console.log(e.order.name);
    });

Broadcast an event to other connected clients without hitting your Laravel application at all

Echo.channel('chat')
    .whisper('typing', {
        name: this.user.name
    });

Laravel Broadcast Documentation

Mccowyn answered 11/10, 2017 at 6:11 Comment(0)
F
0

I think this tutorial is what you want:

Laravel 5 and Socket.io Tutorial

it contains tutorial for client side and server side

and for ios

you could use: Socket.IO-Client-Swift

check this also: Socket.IO on iOS

Favored answered 12/10, 2017 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.