Laravel 5.4 Pusher (laravel-echo) cannot access private channel - laravel-5

I've been testing Laravel Echo and it works fine for non-private channels. I receive the data that gets passed through an event on that non-private channel. But the problem is when I set up the event to broadcast on a private channel I receive an error message as per the below:
Note: I am logged in as a user as per the picture.
As I'm subscribing to a private channel, in Laravel I went to config/app.php and uncommented ' App\Providers\BroadcastServiceProvider::class, '.
Also went to App\Providers\BroadcastServiceProvider.php and used the code as per the picture below:
My Resources/assets/js/bootstrap.js file have the following code and compiled using laravel mix:
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'hiddenkey',
cluster: 'ap1',
encrypted: true
});
window.Echo.private('chat-room.1')
.listen('ChatMessageWasReceived', (e) => {
console.log(e.user, e.chatMessage);
});
btw i already installed all dependencies, ie:
- composer require pusher/pusher-php-server ,
- npm install --save laravel-echo pusher-js ,
Also configured my config/broadcasting.php using the reference to .env file which have inputed all the information needed for pusher. Everything works fine as you can see for a non-private channel which i receive the data from the event as per the picture below:
Here is the event i created:
I am receiving the private channel, event and data as per the picture:
Why even if i am a signed-in authenticated user, and in the BroadcastServiceProvider.php file i am returning true to the Broadcast::channel() method, i don't see the data passed from my event? i receive data from non-private channel, so why not in a private-channel? What are my doing wrong? why do I get these errors in my console and not the data:
- POST http://example.dev/broadcasting/auth 500 (Internal Server Error)
- Pusher : Couldn't get auth info from your webapp : 500
BTW I was following this tutorial until I got stuck with private channel:
https://mattstauffer.co/blog/introducing-laravel-echo

The solution to this problem is that private channels should have a wildcard. In your Providers/BroadcastServiceProvider.php, where defining the the authorisation of your private channel, you should place the wildcard of the private channel in ' {} ' . In my case should be as per below:
Broadcast::channel('chat-room.{chatroomId}', function ($user,
$chatroomId) {} );
Note that even though i defined 'chat-room.1' as the name of my private channel in my event, i still have to use the ' {} ' as per above code for the wildcard and NOT '1', as private channels are supposed to have wildcards. You cannot use * as the wildcard, as was the problem.
Hope it helps anyone.

Related

Laravel echo is not listening to dynamically created one-to-one private channel

SO i am trying to listen to event that create channel dynamically with the the ids of the two users involved. I ma using pusher and echo for this purpose
the event successfully fired from my controller and is being recorded but echo does not listens to that event.
I am using guards as the conversation will be between two admins
My channel.php code is
Broadcast::channel('chatchannel.{reciever_id}.{sender_id}', function ($user, $reciever_id, $sender_id) {
if((int) auth()->guard('admin')->user()->id === (int) $reciever_id || (int) auth()->guard('admin')->user()->id === (int) $sender_id){
return true;
}else{
return false;
}
});
app.js file looks like this
Echo.private('chatchannel.'+app_sender_id+'.'+app_reciever_id)
.listen('.chatEvent', (e) => {
console.log('subscribed');
});
I did this change in my broadcastingerviceprovider.php file according to online soultions but it did not work
Broadcast::routes(['middleware' => 'auth:admin']);
I looked for all the solutions online but could not find anything that is of actual help. Can anyone guide me on how to get it working.
Your broadcast channel is chatchannel.{reciever_id}.{sender_id}.
However your client channel is chatchannel.'+app_sender_id+'.'+app_reciever_id
This means, for a sender of A and receiver of 2 you would have the following channels:
Broadcast - chatchannel.2.A
Client - private-chatchannel.A.2.
Channel names must match for the client to receive the broadcast event. You should ensure the sender and receiver Id are in the same order on both systems and that you are using private channels in both scenarios.

Why Laravel API request sent from Vue gets canceled

I'm trying to send formData from Vue using Axios to Laravel (v6) API on Laravel Homestead containing some data and an excel file to read and validate data in the backend. If the sheet contains records than 800 for example everything works file but when I increased it to 850 or 1000 the request gets cancelled I don't know why.
Successful request
Cancelled request
As you see it doesn't reach the actual request like the successful request it just gets cancelled at the beginning.
But the weird thing is that the data got validated and inserted into the DB already but the response gets converted to cancelled and the actual success response doesn't get returned from the API
Is this error related to server configurations or needs code fix?
Vue code
submitImportedUsers(){
let formData = this.gatherFormData();
this.importUsers(formData);
},
gatherFormData() {
//create formData to send normal dta with files
let formdata = new FormData();
formdata.append("users", this.form.usersFile);
formdata.append("images", this.form.imagesFile);
const json = JSON.stringify({
unique_fields: this.form.uniqueFields,
update_duplicates: this.form.duplicateUsers,
entity_id: this.entityId,
user_id: this.userId,
});
formdata.append("data", json);
return formdata;
Laravel
$usersImportService->moveFile($request->file('users'));
public function moveFile($file) {
if(is_null($this->folderName) || !isset($this->folderName)){
$this->generateUuid();
}
$ext = $this->getRequestFileExt($file);
$usersFileName = $this->folderName.'.'.$ext;
$this->fileName = $usersFileName;
$path = storage_path().'/uploads';
\File::isDirectory($path) or \File::makeDirectory($path, 0777, true, true);
move_uploaded_file($file->getRealPath(), $this->storePath.$usersFileName);
}
Then start reading it using Laravel Excel and insert the data into the database
I found the issue which the front end developer did in axios instance that might help anyone facing this issue
axiosInstance.defaults.timeout = 10000;
It could be anything like that used to set the timeout of axios it's set to 10 seconds here and the request needs more time so setting this to a higher value solved the issue

Vue - Listen to dynamically created channel name

I'm trying to integrate Laravel-Vue-Pusher based notification and laravel's broadcasting documentation has been very helpful.
So we create an laravel event, trigger and broadcast it on a Pusher channel.
On the Javascript side, we use Echo to listen for event broadcasts.
Here's some example-code for from the documentation:
Echo.private(`order.${orderId}`)
.listen('ShippingStatusUpdated', (e) => {
console.log(e.update);
});
I tried using it in Vue and it threw an ReferenceError: orderId is not defined.
On the Laravel side, here's the event that's broadcasting it on that channel:
public function broadcastOn()
{
return new PrivateChannel('order.'.$this->order->id);
}
Event is fired successfully and it also gets logged in Pusher Dashboard.
But i'm unable to figure out why i'm getting missing orderId error in Vue. Any help would be appreciated.

HOW TO: Get real-time notifications in Laravel 4 using Iron.io MQ, Push Queues & AJAX

I've integrated ironMQ push queues in my Laravel 4 app for longer running processes. I have some views that perform a $.get that might take 30 seconds. I wanted to see what others are doing to easily get notified when ironMQ is done pushing back to an endpoint.
An example of what I will be doing with push queues mostly:
public function getCompletedTasks() {
$user = User::find(Auth::user()->id);
Queue::push(function($job) use ($user) {
$recent = new Recent;
$tasks = $recent->getCompletedTasks($user);
// append data from $tasks to DOM
// here's where I want to receive my notification
});
}
Here I am just getting tasks from an API that match data from user.
I know I can store the response data to a database and use AJAX long polling to constantly check for the data but it seems like too much work for most situations I will need to do this. I don't know much about websockets. What types of things have you guys done in these situations? And if you have any examples that would be very helpful. Thanks.
UPDATE: Solved the issue using Pusher. See my answer.
I was able to solve my problem with the help of Pusher. Here's what I did:
Setup my Iron MQ push queue as normal. In routes.php:
Route::post('queue/push', function() {
return Queue::marshal();
});
Installed pusher laravel package.
In my controller then I Queue::push my data. Inside the closure I trigger a new Pusher channel. This will obviously only trigger when the data has been returned from IronMQ.
public function getCompletedTasks() {
$user = User::find(Auth::user()->id);
Queue::push(function($job) use ($user) {
$recent = new Recent;
$tasks = $recent->getCompletedTasks($user);
$pusher = new Pusher('xxx', 'xxx', 'xxx');
$pusher->trigger('reports', 'get_completed_tasks', array('tasks' => $tasks));
$job->delete();
});
});
Next in my view I call my AJAX function with no callback since I won't be doing anything else just yet:
$.get('account/tasks/completed');
Next in my view I initialize Pusher, subscribe to the event and bind get_completed_tasks to the Pusher channel. Now we just wait for a response from Pusher which will then allow me to perform the latter part of my original AJAX request:
{{ HTML::script('//js.pusher.com/2.1/pusher.min.js') }}
<script>
var pusher = new Pusher('xxx');
var channel = pusher.subscribe('reports');
channel.bind('get_completed_tasks', function(data) {
// do something with the data returned
});
</script>
Once I used Pusher in my app, the rest was a breeze. Hope this helps someone!

palm webOS MailService parameter passing problem

i am developing an application in palm webOS. In that application i have to use the MailService to send mail directly without opening any of the email or compose scenes. For that i have to pass params. But i don't know how to pass the params and what params i have to pass to tha MailService.
params.to ="mailId#abc.com";
params.subject = "subj";
params.msg = "message";
this.controller.serviceRequest('palm://com.palm.mail.MailService', {
method: 'messageSend',
parameters: params,
onSuccess: this.messageSentCallback,
onError: this.messageErrorCallback
});
But i am getting error of " Uncaught TypeError: Cannot set property 'to' of undefined,"
can you help me to resolve this problem please.
ThanQ for all.
This looks like a basic Javascript error. Did you put a line like "var params = {};" first to declare the params variable as an empty object?
Do note -- sending email using the service requires that your app access the private system bus as a com.palm.* application. This means you won't be able to distribute via the App Catalog.

Resources