namespace App\Events;
use Duijker\LaravelMercureBroadcaster\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewsItemCreated implements ShouldBroadcast
{
/**
* @var NewsItem
*/
public $newsItem;
public function __construct(NewsItem $newsItem)
{
$this->newsItem = $newsItem;
}
public function broadcastOn()
{
return new Channel('http://example/news-items');
}
}
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cookie;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;
class MercureBroadcasterAuthorizationCookie
{
public function handle(Request $request, Closure $next)
{
/** @var Response $response */
$response = $next($request);
if (!method_exists($response, 'withCookie')) {
return $response;
}
return $response->withCookie($this->createCookie($request->user(), $request->secure()));
}
private function createCookie($user, bool $secure)
{
// Add topic(s) this user has access to
// This can also be URI Templates (to match several topics), or * (to match all topics)
$subscriptions = [
"http://example/user/{$user->id}/direct-messages",
];
$jwtConfiguration = Configuration::forSymmetricSigner(
new Sha256(),
InMemory::plainText(config('broadcasting.connections.mercure.secret'))
);
$token = $jwtConfiguration->builder()
->withClaim('mercure', ['subscribe' => $subscriptions])
->getToken($jwtConfiguration->signer(), $jwtConfiguration->signingKey())
->toString();
return Cookie::make(
'mercureAuthorization',
$token,
15,
'/.well-known/mercure', // or which path you have mercure running
parse_url(config('app.url'), PHP_URL_HOST),
$secure,
true
);
}
}
namespace App\Events;
use Duijker\LaravelMercureBroadcaster\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class DirectMessageCreated implements ShouldBroadcast
{
/**
* @var DirectMessage
*/
public $directMessage;
public function __construct(DirectMessage $directMessage)
{
$this->directMessage = $directMessage;
}
public function broadcastOn()
{
return new Channel(
"http://example/user/{$this->directMessage->user_id}/direct-messages",
true
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.