I have an issue with Laravel Pusher events
first of all
when I fire the event from the pusher debug console it works fine
But when I try to do it from my laravel controllers, it doesn't.
and here is my code
my event
class FileAdded 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('file.add');
}
public function broadcastAs()
{
return 'my-event';
}
}
my pusher client side
Pusher.logToConsole = true;
var pusher = new Pusher('xxxxxxxxxxx', {
cluster: 'eu'
});
var channel = pusher.subscribe('file.add');
channel.bind('my-event', function(data) {
alert('hi');
});
where I'm -supposedly- triggering my event
public function store(Request $request)
{
event(new FileAdded());
//some unrelated stuff happen in that function
}
and here is my console
Pusher : : ["State changed","initialized -> connecting"]
pusher.min.js:8 Pusher : :
["Connecting",{"transport":"ws","url":"wss://ws-eu.pusher.com:443/app/b7f91be243251ea13075?protocol=7&client=js&version=7.0.3&flash=false"}]
pusher.min.js:8 Pusher : : ["State changed","connecting -> connected
with new socket ID 131587.15868131"] pusher.min.js:8 Pusher : :
["Event
sent",{"event":"pusher:subscribe","data":{"auth":"","channel":"file.add"}}]
pusher.min.js:8 Pusher : : ["Event
recd",{"event":"pusher_internal:subscription_succeeded","channel":"file.add","data":{}}]
pusher.min.js:8 Pusher : : ["No callbacks on file.add for
pusher:subscription_succeeded"]
So what could be the problem and thanks in advance!
Related
I have a very simple application that uses Laravel-echo-server with Socket.io for real-time communication, the application is working, the problem is that when I change the message of the event that is going to be executed, it completely ignores all the changes of the php file, for example I have the following class.
class PublicMessage 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('public-message-channel');
}
public function broadcastAs()
{
return 'MessageEvent';
}
public function broadcastWith()
{
return ['message' => "This is a public message"];
}
}
I subscribe as follows in my front end
window.Echo.channel('public-message-channel').listen('.MessageEvent', (data) => {
console.log(data);
});
then i trigger the event from a route
Route::get('/chat', function(){
event(new PublicMessage());
dd("Public event exceute successful.");
});
and i get
{"message": "This is a public message"}
but if i change the return in broadcastWith function i still get the same response, even restarting the php artisan queue:work the laravel-echo-server and my Xampp server, the only thing that makes it detect the changes is restarting the machine I'm developing on, does anyone have an idea what could be going on?
I tring add a chat extension to my web site , watched very much video lesson, Laravel and Pusher using user. Normally website is working
broadcasting(new MyEvent('my-event'));
but if I add line -before return line- , giving 500 Internal Server Error.
sended message is saving to DB but not return value...
Please help me
My ChatEvent.php
use App\Chat;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Broadcasting\PresenceChannel;
....
class ChatEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $chat;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Chat $chat)
{
$this->chat = $chat;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel('chat');
}
}
My ChatController.php
use Illuminate\Http\Request;
use App\Chat;
use App\Events\ChatEvent;
class ChatController extends Controller
{
public function __construct(){
$this->middleware('auth');
}
public function index(){
return view('chat.chat');
}
public function fetchAllMessages(){
return Chat::with('user')->get();
}
public function sendMessage(Request $request){
$chat = auth()->user()->messages()->create([
'message' => $request->message
]);
broadcast(new ChatEvent($chat->load('user')))->toOthers();
return ['status' => 'success'];
}
}
VueJs Post And Get codes
<script>
export default {
methods: {
fetchMessages(){
axios.get('messages').then(response =>{
this.messages = response.data;
})
},
sendMessage(){
this.messages.push({
user: this.user,
message: this.newMessage
});
axios.post('messages',{message: this.newMessage});
this.newMessage='';
},
}
}
</script>
Routes and Channels
Route::get('/chats','ChatController#index');
Route::get('/messages','ChatController#fetchAllMessages');
Route::post('/messages','ChatController#sendMessage');
Broadcast::channel('chat', function ($user) {
return $user;
});
Pusher's AppKey,Secret,ID and Cluster OK,
Broadcaster-Driver: pusher Everywhere
Im making a chat in my app and Im using pusher. I did everything that pusher told me to do. I'm using vanilla-js in frontend. I can connect in frontend. I can receiver messages from pusher (look at channel.bind()) but my messages are not going anywhere.
P.S I did all configuration (.env, broadcasting.php)
FRONTEND
var pusher = new Pusher('xxxxxxxx', {
cluster: 'eu',
forceTLS: false
});
window.channelName = 'my-channel-' + {{ $chat->id }};
window.event = 'private-conversation';
var channel = pusher.subscribe(window.channelName);
channel.bind(window.event, function (data) {
alert(data);
});
BACKEND
public function sendMessage($request, $roomId){
event(new Event(array(
'id' => auth()->user()->id,
'full_name' => auth()->user()->fullName()
), $request->input('channel'), $request->input('event'), $request->input('message')));
}
EVENT
class Event
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $message;
public $channelName;
public $event;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($user, $channelName, $event, $message)
{
$this->user = $user;
$this->channelName = $channelName;
$this->event = $event;
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return [$this->channelName];
}
public function broadcastAs()
{
return $this->event;
}
}
In my app i using from pusher to send notification for typists.but i giving this error:
Couldn't get auth info from your webapp : 500
javascript codes of pusher placed in footer:
<script src="https://js.pusher.com/4.1/pusher.min.js"></script>
<script>
Pusher.logToConsole = true;
var pusher = new Pusher('xxxxxxxxxxxxxxxxx', {
cluster: 'ap2',
encrypted: true,
authEndpoint: "/broadcasting/auth",
auth: {
params: {
'X-CSRF-Token': $('meta[name="csrf-token"]')
.attr('content')
}
}
});
var channel = pusher.subscribe(
'private-App.Typist.' + {{$typistId}}
);
channel.bind('NewTypeOrder', function(data) {
alert('hi');
});
in channels.php
Broadcast::channel('App.Typist.{id}', function (Typist $typist, $id) {
return true;
});
and in Events/EventTypeOrder.php
class NewTypeOrder implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $typist;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Typist $typist)
{
$this->typist = $typist;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('App.Typist.' . $this->typist->id);
}
}
my typists table is quite from users table and laravel authentication for typist not used because laravel authentication just used for users only.
in laravel.log
[2018-07-22 20:27:38] local.ERROR: ErrorException: Key file
"file://C:\xampp\htdocs\samane_typax_5.4\storage\oauth-public.key"
permissions are not correct, should be 600 or 660 instead of 666 in
C:\xampp\htdocs\samane_typax_5.4\vendor\league\oauth2-
server\src\CryptKey.php:57
Stack trace:
Now what can i do for this issue?
On the commandline, type: php artisan tinker
Then paste the following:
chmod(storage_path('oauth-public.key'), 0660)
This should set the right file permissions, the error should vanish/change once enter is pressed :)
I was broadcasting my event with help of pusher,it's worked fine but when i used queue implementation then pusher haven't receive any broadcast or may be event is not broadcasting.I'm not understand what the issue is.Code is given below please help me
Controller function
public function index()
{ $this->user_id=2;
Event::fire(new UpdateDeviceStatus($this->user_id));
}
Event file
class UpdateDeviceStatus extends Event implements ShouldBroadcast
{
use SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public $devices;
public function __construct($id)
{
$this->devices=Device::with('units')->where('user_id',$id)->get();
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return ['update-status'];
}
}
js file
Pusher.logToConsole = true;
var pusher = new Pusher('key', {
encrypted: true
});
var channel = pusher.subscribe('update-status');
channel.bind('App\\Events\\UpdateDeviceStatus', function (data) {
console.log(data);
});
I had the same issue and realised that I just forgot to listen to the queue: php artisan queue:listen redis