500 (Internal Server Error) with pusher in Laravel - laravel

I am working with Laravel 7 with Vue.js, Laravel-echo and pusher.
when i call an event in vue i am receiving 500 (Internal Server Error)
bootstrap.js:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: true
});
vue code
listen: function() {
Echo.channel('commentsChannel')
.listen('NewComment', (comment) => {
// this.comments.unshift(comment);
console.log('yessss');
})
}
Event: NewComment.php
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; //for queueing
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; //for no queueing
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\Comment;
class NewComment implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $comment;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Comment $comment)
{
$this->comment = $comment;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('commentsChannel');
}
}
routes/channels.php
<?php
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Any Solution for this Error?! or How can we debug and know more about this error?!
Thank you in advance for helping!

bootstrap.js: give your key and cluster
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'XXXXXXXXXX',
cluster: 'XXX',
forceTLS: true
});

Related

Laravel echo private channel not working on Nuxt js

I am using Laravel 9, Nuxt js, laravel-websocket. Laravel Echo is working only on public channels not working on private channels.
Here is my code.
channels.php
Broadcast::channel('trades', function ($user) {
return auth()->check();
});
Events file (NewTrade.php)
class NewTrade implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $trade;
public $user;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($trade, User $user)
{
$this->trade = $trade;
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('trades');
}
}
nuxt.config.js
buildModules: [
['#nuxtjs/laravel-echo', {
broadcaster: process.env.BROADCAST_DRIVER,
key: process.env.PUSHER_APP_KEY,
cluster: process.env.PUSHER_APP_CLUSTER,
forceTLS: false,
wsHost: '127.0.0.1',
wsPort: 6001,
wssPort: 6001,
disableStats: true,
authModule: true,
connectOnLogin: true,
encrypted: true,
authEndpoint: process.env.PUSHER_AUTH_ENDPOINT,
}]
],
echo.js (Plugin)
import Pusher from "pusher-js";
Pusher.logToConsole = true;
client echo
this.$echo.private('trades')
.listen('NewTrade', (e) => {
console.log("A testing");
});
Note: If I send data from the public channel client-side received it but the only problem is when I broadcast on the private channel.
I have spent more than 10 hours and tried everything I could, but no luck. :(
Thanks.

Why I am not receving response ?( Details not updating in Laravel Vue js Web Sockets)

(PHP ^7.2.5 and laravel ^7.24 , beyondcode laravel:^1.12pusher/pusher-php-server 5.0.3 Library for interacting with the Pusher REST API
)
Env File
This is my env file. Id and Key values are real and I have used it in bootstrap.js file
PUSHER_APP_ID=myappid
PUSHER_APP_KEY=myappkey
PUSHER_APP_SECRET=myappsecret
PUSHER_APP_CLUSTER=mt1
After following the tutorial from this link https://medium.com/#aschmelyun/adding-real-time-updates-to-your-laravel-and-vue-apps-with-laravel-websockets-a1d9a69f7c12
Event File
class OrderEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels, OtherTrait;
/**
* Create a new event instance.
*
* #return void
*/
public $orders;
public $orderRepo;
public function __construct()
{
$this->orders = 'Hello World';
//return response()->json(ResponseHelper::GenerateResponse(200, '', ['order' => $order]), 200);
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('orders');
}
}
Bootstrap.js File
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'myappkey',
cluster: 'mt1',
forceTLS: false,
encrypted: false,
wsHost: window.location.hostname,
wsPort: 6001,
wssPort: 6001,
disableStats: true,
enabledTransports: ['ws', 'wss']
});
Component File
created() {
window.Echo.channel("orders").listen("OrderEvent", (e) => {
debugger;
console.log(e.orders);
//this.setEchotoWork(e.orders);
});
this.fetchData();
},
if you are getting error related to array_merge
check pusher/pusher-php-server version
using cmd
composer show
if it is not 4.1 run cmd
composer require pusher/pusher-php-server ^4.1

Can't receive events broadcasted on Laravel

I've been struggling for a while now and can't find a way to make this work.
My project is on Laravel 8
I'm using Redis and laravel-echo-server (since it doesn't seem maintained I also tried with a hand-made express/socket.io server)
The problem : When I fire the event, it's received by Redis and Laravel Echo Server but it's never received on my webpage's console.
"Events/SendMessage"
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class SendMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('user-channel');
}
public function broadcastAs()
{
return 'UserEvent';
}
public function broadcastWith()
{
return [
'title' => 'This notification is awesome'
];
}
}
".env"
BROADCAST_DRIVER=redis
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
LARAVEL_ECHO_PORT=6001
"welcome.blade.php"
<script>
window.laravel_echo_port='{{env("LARAVEL_ECHO_PORT")}}';
</script>
<script src="https://cdn.socket.io/3.1.3/socket.io.min.js" integrity="sha384-cPwlPLvBTa3sKAgddT6krw0cJat7egBga3DJepJyrLl4Q9/5WLra3rrnMcyTyOnh" crossorigin="anonymous"></script>
<script src="{{ ('/js/laravel-echo-setup.js') }}" type="text/javascript"></script>
<script>
window.Echo.channel('user-channel').listen('.SendMessage', (e) => {
console.log('Got event...');
console.log(e);
});
</script>
"laravel-echo-setup.js"
import Echo from 'laravel-echo';
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ":6001"
});
Hope someone can help me
If you need anymore code, I'm here !!

Pusher Receive Data Null

I am using laravel 5.8 version. I'm sending message and user value to pusher but put pusher receive event
{"message": null, "user": null}.
Here is my broadcasting.php file
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
// 'cluster' => env('PUSHER_APP_CLUSTER'),
// 'useTLS' => true,
'cluster' => 'ap2',
'useTLS' => true
],
],
Here is my chatevent.php file.
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Auth\User;
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\ShouldBroadcast;
class ChatEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public $user;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($message, User $user)
{
$this->$message = $message;
$this->$user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
And there is my bootstrap.js file.
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
// broadcaster: 'pusher',
// key: process.env.MIX_PUSHER_APP_KEY,
// cluster: process.env.MIX_PUSHER_APP_CLUSTER,
// encrypted: truebroadcaster: 'pusher',
broadcaster: 'pusher',
key:'7d64c4828272fba9bbdd',
cluster: 'ap2',
encrypted: true
});
My controller function where I'm sending message and user to pusher.
public function send()
{
$message = 'hello';
$user = User::find(Auth::id());
event(new ChatEvent($message, $user));
}
}
Add payload inside your chatEvent class: and also you are accessing data members in wrong way write:
$this->message = $message; //right way
instead of:
$this->$message = $message; //wrong way,(remove dollar sign when using $this->)
write code as follows: you can replace message variable with payload also:
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Auth\User;
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\ShouldBroadcast;
class ChatEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public $user;
public $payload;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($message, User $user, $payload)
{
$this->message = $message;
$this->user = $user;
$this->payload = $payload;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

Laravel Socket.I.O vuejs

hi i am trying to connect with Socket.I.O vuejs laravel
in my App.js or Main.js code is
import io from 'socket.io-client';
import Echo from 'laravel-echo';
import VueEcho from 'vue-echo';
import VueSocketio from 'vue-socket.io';
window.io = require('socket.io-client');
const EchoInstance = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});
Vue.use(VueEcho, EchoInstance);
In Channels.php
Broadcast::channel('test-event', function() {
return true;
});
Example event
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\ShouldBroadcast;
class ExampleEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct()
{
return "Hello";
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('test-event');
}
public function broadcastWith()
{
return [
'data' => 'key'
];
}
}
in Routes web.php
Route::get('test-broadcast', function(){
broadcast(new \App\Events\ExampleEvent);
});
in my Components file
mounted() {
this.$echo.channel("test-event").listen("ExampleEvent", e => {
console.log(e);
});
}
according to
https://medium.com/#dennissmink/laravel-echo-server-how-to-24d5778ece8b
when i run
/test-broadcast
it have to give response in my about.vue browser console , but there is nothing to display at all. can any help what thing i am missing yet , Thanks in Advance
have you setup any queue driver? you will need to configure and run a queue listener. All event broadcasting is done via queued jobs.
or else you can use ShouldBroadcastNow interface.
change in you Example Event:
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class ExampleEvent implements ShouldBroadcastNow
{ ...

Resources