Dispatch an event using either event or broadcast helpers
Event:
<?php
namespace App\Content\Events;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Database\ModelIdentifier;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\Channel;
use App\Content\Models\SimpleUser;
class FeedNotificationWasReceived implements ShouldBroadcast
{
use SerializesModels;
public $notification;
public $user;
public function __construct($notification, $user=null)
{
$this->notification = $notification;
$this->user = $user ?: \Auth::getUser();
if(!$this->user instanceof SimpleUser) {
$this->user = SimpleUser::apiFind(\Auth::getUser()->id);
}
}
public function broadcastOn()
{
// return $this->notification->channels;
return array_map(function ($channel) {
return new Channel($channel);
}, (array)$this->notification->channels);
}
}
Dispatch Event:
public function created($model)
{
$notification = $this->prepareNotification($model);
if($notification) {
try {
broadcast($notification)->toOthers();
\Log::info("Sent notification on new activity");
} catch (\Exception $e) {
\Log::warning("Error sending notification: ".$e);
}
}
}
The following error is shown:
[20:29:20] LOG.warning: Error sending notification: Illuminate\Contracts\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ <required> $event ]] in class Illuminate\Broadcasting\BroadcastEvent in /Projects/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php:933
Stack trace:
#0 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(871): Illuminate\Container\Container->unresolvablePrimitive(Object(ReflectionParameter))
#1 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(812): Illuminate\Container\Container->resolvePrimitive(Object(ReflectionParameter))
#2 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(779): Illuminate\Container\Container->resolveDependencies(Array)
#3 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(631): Illuminate\Container\Container->build('Illuminate\\Broa...')
#4 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(586): Illuminate\Container\Container->resolve('Illuminate\\Broa...', Array)
#5 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(721): Illuminate\Container\Container->make('Illuminate\\Broa...', Array)
#6 /Projects/public_html/vendor/october/rain/src/Foundation/Application.php(162): Illuminate\Foundation\Application->make('Illuminate\\Broa...')
#7 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(177): October\Rain\Foundation\Application->make('Illuminate\\Broa...')
#8 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(164): Illuminate\Queue\Jobs\Job->resolve('Illuminate\\Broa...')
#9 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Queue/FailingJob.php(33): Illuminate\Queue\Jobs\Job->failed(Object(Illuminate\Contracts\Container\BindingResolutionException))
#10 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php(119): Illuminate\Queue\FailingJob::handle('sync', Object(Illuminate\Queue\Jobs\SyncJob), Object(Illuminate\Contracts\Container\BindingResolutionException))
#11 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php(46): Illuminate\Queue\SyncQueue->handleException(Object(Illuminate\Queue\Jobs\SyncJob), Object(Illuminate\Contracts\Container\BindingResolutionException))
#12 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(44): Illuminate\Queue\SyncQueue->push('Illuminate\\Broa...', Array, NULL)
#13 /Projects/public_html/vendor/october/rain/src/Events/Dispatcher.php(272): Illuminate\Queue\Queue->pushOn(NULL, 'Illuminate\\Broa...', Array)
#14 /Projects/public_html/vendor/october/rain/src/Events/Dispatcher.php(228): October\Rain\Events\Dispatcher->broadcastEvent(Object(App\Content\Events\FeedNotificationWasReceived))
#15 /Projects/public_html/vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php(57): October\Rain\Events\Dispatcher->dispatch('Ewaste\\Ewaste\\E...')
#16 /Projects/public_html/plugins/ewaste/ewaste/observers/ActivityObserver.php(15): Illuminate\Broadcasting\PendingBroadcast->__destruct()
Broadcast an event using event helpers;
422
This code mirrors Laravel exactly so I'm not sure it relates to October...
Understood; however this was working before the update to Laravel 5.5. Are you able to broadcast events properly? I'm not doing anything special here.
Checking for an update on this. For most issues I can implement a fix but cannot find a way around this through the October Rain library.
@malcolm-bklyn does your code example work currently on just a Laravel 5.5 instance with no October involved at all? This will help us isolate the issue.
@LukeTowers Gonna test this with vanilla Laravel 5.5
I have tested this with Laravel 5 and notifications are delivered to Pusher with no problem:
<?php
namespace App\Events;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Database\ModelIdentifier;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\Channel;
class FeedNotificationWasReceived implements ShouldBroadcast
{
use SerializesModels;
public $notification;
public $user;
public function __construct($notification, $user)
{
$this->notification = $notification;
$this->user = $user;
}
public function broadcastOn()
{
return array_map(function ($channel) {
return new Channel($channel);
}, (array)$this->notification->channels);
}
}
Route::get('/notify', function (Request $request) {
$user = User::firstOrCreate([
'name' => 'Malcolm P',
'email' => '[email protected]',
'password' => 'password'
]);
$notification = FeedNotification::create([
'activity_id' => 1,
'user_id' => $user->id,
'user' => $user->toArray(),
'channels' => ['feed-malcolm-bklyn'],
'message' => "Testing notifications",
'created_at' => strtotime('now')
]);
$notification->save();
$event = new FeedNotificationWasReceived($notification, $user);
broadcast($event);
return "Success";
});
As expected the notification was received properly on Pusher.com with no errors.
@malcolm-bklyn could you dig deeper into the October core code to figure out what's happening?
The support demand for October is increasing, while the available time the founders have to give remains the same. These issues will be handled in time, however there are ways you can help:
I had the same error but I think I found the problem, made a pull request: https://github.com/octobercms/library/pull/275
Same error for me, hopefully the PR above works!
@malcolm-bklyn @kOld @AugmentBLU please confirm that https://github.com/octobercms/library/commit/e9c2144ff73b5d8909cd531bf720185109d60cc4 solves your problem
@LukeTowers Yes This resolves the issue!
Seems to work for me too
Most helpful comment
Seems to work for me too