Add broadcast events for real time updates

This commit is contained in:
Daniel Supernault 2019-04-08 14:38:27 -06:00
parent 5fe8ec5476
commit cb202c9671
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 108 additions and 0 deletions

51
app/Events/NewMention.php Normal file
View file

@ -0,0 +1,51 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use App\User;
class NewMention implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
protected $user;
protected $data;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user, $data)
{
$this->user = $user;
$this->data = $data;
}
public function broadcastAs()
{
return 'notification.new.mention';
}
public function broadcastOn()
{
return new PrivateChannel('App.User.' . $this->user->id);
}
public function broadcastWith()
{
return ['id' => $this->user->id];
}
public function via()
{
return 'broadcast';
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace App\Events\Notification;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use App\Status;
use App\Transformer\Api\StatusTransformer;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class NewPublicPost implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
protected $status;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Status $status)
{
$this->status = $status;
}
public function broadcastAs()
{
return 'status';
}
public function broadcastOn()
{
return new Channel('firehost.public');
}
public function broadcastWith()
{
$resource = new Fractal\Resource\Item($this->status, new StatusTransformer());
$res = $this->fractal->createData($resource)->toArray();
return [
'entity' => $res
];
}
public function via()
{
return 'broadcast';
}
}