Skip to content

Instantly share code, notes, and snippets.

@dbpolito
Last active January 11, 2016 18:22
Show Gist options
  • Save dbpolito/fe384eac8cc8a309cf49 to your computer and use it in GitHub Desktop.
Save dbpolito/fe384eac8cc8a309cf49 to your computer and use it in GitHub Desktop.
Laravel Event Prototype
<div id="messages" class="container spark-screen">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Messages</div>
<div class="panel-body">
<p
v-for="message in messages"
><strong>@{{message.from_user.name}}</strong>: @{{ message.message }}</p>
<input
v-model="message"
@keyup.enter="send"
>
</div>
</div>
</div>
</div>
</div>
var socket = io('http://homestead.app:6001'),
messages = new Vue({
el: '#messages',
data: {
message: '',
messages: [],
},
methods: {
send: function() {
var message = {
from_user: {
name: 'client',
},
from_user_id: 1,
to_user_id: 1,
message: this.message,
};
socket.emit('message:App\\Events\\MessageCreate', message);
this.message = '';
},
add: function(message) {
this.messages.push(message);
}
},
created: function() {
socket.on('message:App\\Events\\MessageCreated', function(data) {
var message = data.message;
this.add(message);
}.bind(this));
},
});
<?php
namespace App\Events;
use App\Message;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class MessageCreate extends Event
{
public $data;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
}
<?php
namespace App\Events;
use App\Message;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class MessageCreated extends Event implements ShouldBroadcast
{
use SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['message'];
}
}
<?php
namespace App\Listeners;
use App\Message;
use App\Events\MessageCreate;
use App\Events\MessageCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class MessageListener
{
/**
* Handle the created event.
*
* @param MessageCreate $event
* @return void
*/
public function create(MessageCreate $event)
{
$message = Message::create($event->data);
event(new MessageCreated($message));
}
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe($events)
{
$events->listen(
'App\Events\MessageCreate',
'App\Listeners\MessageListener@create'
);
}
}
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var middleware = require('socketio-wildcard')();
var Redis = require('ioredis');
var redis = new Redis();
var pub = new Redis();
io.use(middleware);
app.listen(6001, function() {
console.log('Server is running!');
});
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {
socket.on('*', function(message) {
var channelEvent = message.data[0].split(':'),
channel = channelEvent[0],
event = channelEvent[1],
data = {
event: event,
data: message.data[1],
};
pub.publish(channel, JSON.stringify(data));
});
});
redis.psubscribe('*', function(err, count) {
//
});
redis.on('pmessage', function(subscribed, channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
console.log('Emitted: ', channel + ':' + message.event, message.data);
});
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Redis;
class SocketListener extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'socket:listener';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Socket listener';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Listening the socket');
Redis::psubscribe('*', function($message) {
$message = json_decode($message, true);
$event = $message['event'];
$data = $message['data'];
// Do not trigger broadcastable events
if (in_array(ShouldBroadcast::class, class_implements($event))) {
return;
}
event(new $event($data));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment