Laravel Montréal #11 - October 15th, 2015
Thanks for comming !
Sponsors
Jobs
Get involved
BROADCAST_DRIVER=pusher
PUSHER_KEY=0aba70d92427d26e79c3
PUSHER_SECRET=df1398e5ef9929ecc1f6
PUSHER_APP_ID=98291
composer require pusher/pusher-php-server
public function bid($productId, Request $request)
{
$lastBid = Bid::whereProductId($productId)->orderBy('amount', 'desc')->first();
if (isset($lastBid) and $request->get('amount') <= $lastBid->amount)
return redirect('/'.$lastBid->product->slug)->with('errors', collect(['You must bid a higher amount, idiot...']));
$bid = Bid::create($request->all());
Event::fire(new BidWasCreated($bid));
return redirect('/'.Product::find($productId)->slug)->with('success', collect(['Well done my friend, keep on bidding']));
}
namespace App\Events;
class BidWasCreated extends Event implements ShouldBroadcast
{
use SerializesModels;
public $bid;
public function __construct(Bid $bid)
{
$this->bid = $bid;
}
public function broadcastOn()
{
return ['all-users'];
}
}
src="//js.pusher.com/3.0/pusher.min.js"
var pusher = new Pusher('0aba70d92427d26e79c3');
var channel = pusher.subscribe('all-users');
channel.bind('App\\Events\\BidWasCreated', function(message) {
// your logic goes here
$('#bids').prepend('YOUT HTML GOES HERE');
});
BROADCAST_DRIVER=redis
"dependencies": {
"ioredis": "^1.9.1",
"laravel-elixir": "^3.0.0",
"socket.io": "^1.3.7"
}
composer require predis/predis pda/pheanstalk && npm install
public function bid($productId, Request $request)
{
$lastBid = Bid::whereProductId($productId)->orderBy('amount', 'desc')->first();
if (isset($lastBid) and $request->get('amount') <= $lastBid->amount)
return redirect('/'.$lastBid->product->slug)->with('errors', collect(['You must bid a higher amount, idiot...']));
$bid = Bid::create($request->all());
Event::fire(new BidWasCreated($bid));
return redirect('/'.Product::find($productId)->slug)->with('success', collect(['Well done my friend, keep on bidding']));
}
namespace App\Events;
class BidWasCreated extends Event implements ShouldBroadcast
{
use SerializesModels;
public $bid;
public function __construct(Bid $bid)
{
$this->bid = $bid;
}
public function broadcastOn()
{
return ['all-users'];
}
}
var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('all-users');
redis.on('message', function(channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
node socket.js
src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js"
var socket = io('{{env('APP_URL')}}:3337/');
socket.on('all-users:App\\Events\\BidWasCreated', function(message) {
// your logic goes here
$('#bids').prepend('YOUT HTML GOES HERE');
});