-
Notifications
You must be signed in to change notification settings - Fork 655
Fix Symfony deprecation error in TriggerEventController #1147
Conversation
PR Summary
|
|
Works for me. After upgrading laravel/dusk from 7.9.1 to 7.9.2 via dependabot last week the websockets stopped working. I could verify with Here is the list of updates that came in by merging the dependabot PR for laravel/dusk 7.9.2 and as we can see there are some symfony patches and a laravel/framework update.
|
optional(..) returns an object of type
and optional() is never null so using it with null coalesce isn't correct either and an empty array will never be returned. The error may be gone but the channels shouldn't receive anything. |
Sorry for the confusion. The line of code you suggested works. 👍
I didn't try the optional. Everything looks good now and happily updates via websockets in the frontend. |
@pajkho, thank you very much for your PR. Could you adapt your change to aalyusuf's suggestion? Then, I think we are one step closer for it to actually being merged and fixed. $channels = $payload->all()['channels'] ?? []; |
It starts breaking with laravel/framework v10.17.0 |
@ManuelLeiner Please any update on this? |
Sorry for the late reply. I have updated the code now as suggested. |
I am not really involved in this project. @mpociot Moin Marcel, what do you think? Is it something that needs to be fixed here? |
A workaround for Laravel 10 is to downgrade the framework to v10.16.1. composer require laravel/framework:10.16.1 You might have to downgrade some other dependencies, e.g. Jetstream, as well. |
For anyone, who is still looking into how to fix this in their own application without waiting for a new release, the following approach could be used: In your Laravel app, create a copy of the <?php
namespace App\Websockets\Server\Controllers;
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\Controller;
use Illuminate\Http\Request;
class TriggerEventController extends Controller
{
public function __invoke(Request $request)
{
$this->ensureValidSignature($request);
$payload = $request->json();
if ($payload->has('channel')) {
$channels = [$payload->get('channel')];
} else {
$channels = $payload->all()['channels'] ?? [];
if (is_string($channels)) {
$channels = [$channels];
}
}
foreach ($channels as $channelName) {
$channel = $this->channelManager->find($request->appId, $channelName);
optional($channel)->broadcastToEveryoneExcept([
'channel' => $channelName,
'event' => $request->json()->get('name'),
'data' => $request->json()->get('data'),
], $request->json()->get('socket_id'));
DashboardLogger::apiMessage(
$request->appId,
$channelName,
$request->json()->get('name'),
$request->json()->get('data')
);
StatisticsLogger::apiMessage($request->appId);
}
return (object) [];
}
} Then we create a new <?php
namespace App\Websockets\Server;
use App\Websockets\Server\Controllers\TriggerEventController;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelController;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelsController;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsersController;
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
class Router extends \BeyondCode\LaravelWebSockets\Server\Router
{
public function echo()
{
$this->get('/app/{appKey}', WebSocketHandler::class);
$this->post('/apps/{appId}/events', TriggerEventController::class);
$this->get('/apps/{appId}/channels', FetchChannelsController::class);
$this->get('/apps/{appId}/channels/{channelName}', FetchChannelController::class);
$this->get('/apps/{appId}/channels/{channelName}/users', FetchUsersController::class);
}
} Finally, in your $this->app->singleton('websockets.router', function () {
return new \App\Websockets\Server\Router();
}); Restart your websocket:serve command and you should be able to send broadcasts again! |
Thank you for the PR and sorry for the delay in merging this 🙈 |
Should fix #1146 that is caused by Symfony deprecating non-scalar values in InputBag.php.