Why Laravel Pusher is not working in server - php

I am using Pusher in my Web Application. My backend is built with Laravel & Frontend is built with Angular 10. Everything works perfectly in my local machine. But in the server it is not working. The client is failing to connect stating the following error:
Pusher : : [{"type":"WebSocketError","error":{"type":"PusherError","data":{"code":4001,"message":"Could not find app key `4895a6a5e4626a3afdfb`."}}}]
In my local machine, everything is working properly. Both in the angular and the laravel project, I am using the same app key. And the keys are correctly collected from the pusher website.
Here is my Client Connecting Code:
constructor() {
console.log(window.location.hostname);
this.pusher = new Pusher(environment.pusher.key, {
cluster: environment.pusher.cluster,
wsHost: '162.214.125.121',
// wsHost: '127.0.0.1',
wsPort: 6001,
encrypted: false,
forceTLS: false,
});
this.channel = this.pusher.subscribe('draw_options');
Pusher.log = (msg) => {
console.log(msg);
};
}
export const environment = {
production: false,
pusher: {
key: '4895a6a5e4626a3afdfb',
cluster: 'mt1',
}
};
And in my laravel env:
PUSHER_APP_ID=1069575
PUSHER_APP_KEY=4895a6a5e4626a3afdfb
PUSHER_APP_SECRET=__its_correct_as_well__
PUSHER_APP_CLUSTER=mt1
in my broadcasting.php
'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' => false,
'encrypted' => false,
// 'host' => '162.214.125.121',
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http'
],
],
everything is working properly in the local machine. But in server this fails
If you go to the following url, you will see, socket is getting connecting by laravel.
http://api.etakweet.com/laravel-websockets
I have also noticed, no logs were found when event is getting broadcasted by laravel project.

Managed to connect by running the following command.
php artisan config:clear

Related

Laravel Pusher broadcast data as array

When trying to send a request to Pusher from my controller, I would like to see a array in the channel as follows:
[3,"e7563ce0-c1ef-499d-9919-9a3d2a3ff523",{"status":"Accepted","currentTime":"2022-06-22T13:16:05+0000","interval":30}]
The way I am sending an request looks like this (ChargerController.php)
$pusher = new Pusher(
$charger->identifier,
$charger->identifier,
$charger->identifier,
[
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => false,
'encrypted' => false,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http',
]
);
$pusher->trigger($charger->getOcppChannelName(), 'Reset', json_encode([
OCPPMessageService::OCPP_MESSAGE_TYPE_CALL,
Uuid::uuid4(),
'Reset',
['type' => ChargerResetTypeEnum::HARD],
]), [], true);
For some reason this gives the following result:
{"0":2,"1":"1a00b2f6-5af9-4a56-bd1c-723b6669380d","2":"Reset","3":{"type":"Hard"}}
I would like to have the data be an array, without any keys, as the system we are interacting with doesn't understand an object with keys. Even in the vendors we weren't able to find the reason it does this.

Laravel websockets AWS EC2 - Connection failed

I'm using laravel-websockets and is working like a charm in my local docker machine but I can't seem to make it work when I deploy it in the AWS EC2 machine.
This is the error message I get:
pusher.min.js:8 WebSocket connection to
'wss://sample.project:6001/app/b0901feacd04936e?protocol=7&client=js&version=4.3.1&flash=false' failed:
These are my config files:
broadcasting.php
'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'),
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http',
],
],
websockets.php
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'enable_client_messages' => false,
'enable_statistics' => true,
],
],
bootstrap.js
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
wsHost: window.location.hostname,
wsPort: 6001,
forceTLS: false,
disableStats: true,
});
I've opened 6001 port in my security group:
But I can't make it work. I've tried a lot of the fixes in other SO questions but none worked, am I missing something? In desperate need of help here.
Thank you very much.
Your SSL settings in config/websockets.php should indicate they require two variables fron the .env file.
'ssl' => [
'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),
'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),
'passphrase' => null,
'verify_peer' => false,
],
Then over in your .env file, for local (in Valet) specify your test site's certs, ie:
LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="/users/grant/.config/valet/Certificates/localsite.test.crt"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="/users/grant/.config/valet/Certificates/localsite.test.key"
Alternatively, if it's on your Production environment, point it to your production SSL CRT & key:
LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="/etc/nginx/ssl/yourprodsite.com/1231342/server.crt"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="/etc/nginx/ssl/yourprodsite.com/1231342/server.key"
Your bootstrap.js (or similar) receiving JS end wouldn't really change - just ensure it matches what's in config/broadcasting.php:
import Echo from 'laravel-echo';
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'yoursite-pusher-key',
wsHost: window.location.hostname,
wsPort: 6001,
wssPort: 6001,
disableStats: true,
});
Config counterpart config/broadcasting.php inside of the connections array:
'pusher' => [
'driver' => 'pusher',
'key' => 'yoursite-pusher-key',
'secret' => 'yoursite-secret',
'app_id' => 'yoursite',
'options' => [
'cluster' => 'main-cluster',
'encrypted' => true,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'https',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
Personally I could not get my setup to work without SSL & had to provide the SSL certificates accurately. Don't forget to use valet secure locally, if using Valet.
You can have a look here for more info regarding SSL.
Once you serve the Websockets using php artisan websockets:serve it's OK if it seems like it's stalling, navigate over to the frontend & hard refresh (CMD+SHIFT+R) or Win (CTRL+F5) and hopefully the connection will be established.
Ensure you are using latest libraries and if you have set up your JavaScript init scripts inside of a bootstrap you may need to run npm run dev or similar to compile assets.

Laravel WebSockets: Illuminate \ Broadcasting \ BroadcastException: No Message and net::ERR_CERT_AUTHORITY_INVALID

I've been trying to get this WebSocket package running on my local Laravel project for about a week now and I can't seem to find a solution to the issue. I've followed the documentation, multiple tutorials and answers on Stack Overflow / Github, but I keep getting the same error. These are my configurations:
.env:
PUSHER_APP_ID=anyId
PUSHER_APP_KEY=anyKey
PUSHER_APP_SECRET=anySecret
PUSHER_APP_CLUSTER=eu
config/broadcasting:
'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,
'encrypted' => false,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
]
config/websockets:
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'path' => env('PUSHER_APP_PATH'),
'capacity' => null,
'enable_client_messages' => false,
'enable_statistics' => true,
],
],
resources/js/bootstrap.js:
There is also WebsocketEvent and controller which calls the broadcasting of the event, I haven't included them to keep this shorter.
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'anyKey',
encrypted: false,
wsHost: window.location.hostname,
wsPort: 6001,
disableStats: true,
enabledTransports: ['ws', 'wss']
});
window.Echo.channel('DemoChannel')
.listen('WebsocketEvent', (e) => {
console.log(e);
});
Front-end error: This only seems to be fine when I set enabledTransports: ['wss'] which represents the secure connection but default of enabledTransports: ['ws'] doesn't.
Back-end error: Always fails.
As you can see, I've added the curl options as recommended on https://github.com/beyondcode/laravel-websockets/issues/109 and I've set encrypted: false. Overall, I believe it is a certificate issue which is beyond my full comprehension but the tutorials I followed seem to get it working perfectly on their local environment without SSL certificates, etc. Also, it seems like other developers who are having similar issues are not getting answered (example), which is why I am posting this. Any help would be appreciated, thank you.

beyondcode\laravel-websockets it is redirecting to wss instead of ws on localhost

I am using laravel version 5.7 and beyondcode\laravel-websockets package v1.3 the problem I am facing right now is when I am running it on localhost I am getting this error
WebSocket connection to 'wss://localhost/app/somekey?protocol=7&client=js&version=6.0.2&flash=false' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID
I have changed encrypted to false in bootsrap.js still it connects to wss instead of ws
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: false,
wsHost: window.location.hostname,
wsPort: 6001,
disableStats: true
});
broadcasting.php config
'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'),
'encrypted' => false,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http'
],
],
Add this line to the config,dont forget to compile
enabledTransports: ['ws'],
If this doesnt work try to downgrade pusher.js to 4.3
In case somebody else sees this, I've solved it once I stopped going through https://www.mypage.com and use http://www.mypage.com
Just change the protocol on your browser from https to http

Laravel websockets failed to connect in pusher

When I run my project, I got an exception error but it does not have a clear message. I only have this kind of the main body error
I can access websocket admin page at http://127.0.0.1:8000/laravel-websockets but when i go http://127.0.0.1:8000/ i got errors below.
The following error was encountered while trying to retrieve the URL: http://127.0.0.1:6001/apps/995591/events?
Connection to 127.0.0.1 failed.
The system returned: (111) Connection refused
The remote host or network may be down. Please try the request again.
Generated Tue, 05 May 2020 17:12:03 GMT by proxyserversetup-s-1vcpu-1gb-sgp1-07 (squid/3.5.27)
I followed every single thing in the docs from this link
Here are some of my config
broadcasting.php
'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'),
'encrypted' => false,
'useTLS' => true,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
],
],
],
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=inventory
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=995591
PUSHER_APP_KEY=644a4ac1988060882370
PUSHER_APP_SECRET=739696537f4fb23b8fcd
PUSHER_APP_CLUSTER=ap1
I am using laravel 6.x and the current version for laravel websockets.
Is it my ISP cause the error?
I had something similar to this happen to me.
If you are using localhost, change
'useTLS' => true,
to false
'useTLS' => false,
It should be in your 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' => false,
],
],
....
Making sure you set scheme to https and use 'useTLS' => true then setting up curl_options like below if you set up the paths for SSL certificate & private key.
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
],
Verify the PUSHER_PORT data in the .env file:
PUSHER_HOST=127.0.0.1
PUSHER_PORT=6379
Verify using CLI in the docroot with artisan command for web socket:
php artisan websockets:serve --port=6379

Categories