5

I am building an app that gives students the ability to see their schedule which is created by the admin. Now each student has a group_id. I want to make the schedule update in real time so I applied this tutorial http://www.kodeinfo.com/post/realtime-app-using-laravel-nodejs-angularjs-redis . here what I've done so far.

Event Handler:

namespace echooly\Handlers;
use Redis;
use Response;

class StudentScheduleUpdatedEventHandler 
{

    CONST EVENT = 'schedule.update';
    CONST CHANNEL = 'schedule.update';

    public function handle($data)
    {
        $redis = Redis::connection();
        $redis->publish(self::CHANNEL, $data);

    }

}

AdministrationController (Event CRUD Method)

//Create an event
public function createEvent()
{

     if(Auth::Admin()->check()) {
        $eventDetail = Input::all();
        $event = Planing::create($eventDetail);
        $event->save();
        Event::fire(\echooly\Handlers\StudentScheduleUpdatedEventHandler::EVENT, array($event));
    } else {
        return Redirect::intended('/');
    }
}

So Basically I am pushing the latest created event.

Node Server:

var express  = require('express'),
       http  = require('http'),
     server  = http.createServer(app);
var app      = express();
const redis  = require('redis');
const io     = require('socket.io');
const client = redis.createClient();

server.listen(3000, 'localhost');
console.log("Listening.....");

io.listen(server).on('connection', function(client) {
     const redisClient = redis.createClient();

     redisClient.subscribe('schedule.update');

     console.log("Redis server running.....");

     redisClient.on("message", function(channel, message) {
         client.emit(channel, message);
     });
     client.on("getGroup", function(groupId) {
         console.log(groupId);
     });

    client.on('disconnect', function() {
         redisClient.quit();
    });
});

Angular Controller:

  studentSocket.on('schedule.update', function (data) {
        $scope.events.length = 0;
        $scope.populatePlan(JSON.parse(data));
        inform.add('New Course Added');
  });

The issue is how can I filter the sent data to a specific student.

  1. Something is telling me that we can make a dynamic channel with redis? sadly I don't have any experience with it.
  2. I tried to fire the event when I fetch the data by the student group id this ends up making the student see a new schedule whenever the group id changes.

//Show Planing by group
public function showPlaningsByGroup($id){
    if(Auth::Admin()->check()){
        $planing = Planing::with('Course')->where('group_id',$id)->get();
        Event::fire(\echooly\Handlers\StudentScheduleUpdatedEventHandler::EVENT, array($planing));
        return Response::json($planing);
    }
}

I hope I was clear enough I really hope to get some answers thanks.

Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
Yassine
  • 55
  • 1
  • 9

1 Answers1

0

You need to send the data through to each student on a separate channel.

Example:

public function broadcastOn()
{
    return ['Student.' . $this->student->Id];
}
Lance Pioch
  • 1,147
  • 6
  • 18