Connection timed out Laravel-echo-server - laravel

I know and I have seen this question
Getting ERR_CONNECTION_TIMED_OUT Laravel echo server
But I tried the solution in comment but it doesn't work.
I have my Laravel-echo-server.json
{
"authHost": "https://mysite.it",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {
"host":"0.0.0.0",
"port":"6379",
"password": "mypwd"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "https",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "path_cert" ,
"sslKeyPath": "path_key" ,
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*",
"allowMethods": "GET, PoST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested -With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-IdOrigin, Content-Type, X-A uth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-IdOri gin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF -TOKEN, X-Socket-Id"
}
}
And I have in my bootstrapjs the connection with Frontend:
import Vue from 'vue'
import Echo from 'laravel-echo'
window.io = require('socket.io-client')
export var echo_instance = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
auth: {
headers: {
'Authorization': "Bearer " + localStorage.getItem('token')
}
}
})
Vue.prototype.$echo = echo_instance
export default Vue
I have check if the port was closed but with netstat -an I can see:
tcp6 0 0 :::6001 :::* LISTEN
But when I try to launch my app i receive:
app.js:569 GET
https://mysite.it:6001/socket.io/?EIO=3&transport=polling&t=NKoWZoK
net::ERR_CONNECTION_TIMED_OUT
How can I use the laravel echo with https protocol?

this might be because you have the wrong access token(dynamically added). why don't you try using a static access token first (copy the access token and paste it instead of localStorage.getItem('token')) for the user and see if it works.
Also try putting the following in laravel-echo-server.json file
"allowOrigin": "http://mysite.it:80"
And, see if APP_URL is different in .env file

Related

NuxtJS + Laravel8 + laravel echo server socket io = subscription_error

I have this weird issue I can't fix with authenticating private channels. I am using Laravel 8, NuxtJS (SPA app), Redis, and separate Docker containers for everything.
When I am trying to listen in my SPA NuxtJs app for the private channel I get the following error:
42["subscription_error","private-robert-test-1",0]
I assume this is because of my echo confing in nuxt.config.js:
echo: {
broadcaster: 'socket.io',
host: 'http://publisher-local.co.uk:8080',
// authModule: true,
// disconnectOnLogout: true,
// token: this.token
},
Then the call looks like this(in the ws tab in my browser):
42["subscribe",{"channel":"private-robert-test-1","auth":{"headers":{}}}]
I assume it should have something in the auth object. So I followed the documentation of NuxtJS Laravel Echo Server package and tried to set authModule to true. As soon as I do that I get this weird error:
Now I imagine that if it worked then auth module would simply add a token to the subscription message I showed before (the one with an empty auth key).
In Laravel I have already done this Broadcast::routes(['middleware' => ['auth:api']]); in the BroadcastServiceProvider.
Although I don't think I am even getting there.
This is my Laravel echo server config file.
{
"apiOriginAllow": {},
"authEndpoint": "/broadcasting/auth",
"authHost": "http://publisher-local.co.uk:8080/api/v1",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {
"host": "redis",
"port": "6379",
"keyPrefix": "",
"options": {
"db": "0"
}
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
},
"publishPresence": true
},
"devMode": false,
"host": null,
"port": 6001,
"protocol": "http",
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"socketio": {},
"subscribers": {"http": true, "redis": true}
}
Public channels on the other hand work just fine.
What am I missing here? Thank you in advance.
EDIT:
I have also tried creating a plugin for echo where I could populate information I need myself and it looks like this:
export default function ({ $echo, $auth}) {
console.log($echo.connector.options.auth)
let authData = {
headers: {
Authorization: $auth.strategy.token.get(),
Accept: 'application/json',
"X-Requested-With": 'XMLHttpRequest'
},
};
$echo.connector.options.auth = authData;
$echo.options.auth = authData;
}
So I do have the token now and other headers I can see them in the WS tab but it does not make any difference.
Probably not relevant anymore.. But if so..
You should update your version of laravel echo to v2* => https://github.com/nuxt-community/laravel-echo-module/releases/tag/v2.0.0-alpha.5

Laravel-echo receives events but doesn't join channel listeners

To start with, I have a real-time chat app (Vue as frontend, Laravel as backend)
On localhost it works perfectly, but not on production.
I've noticed that I do receive events, but I've never been able to join any channel.
Here is all the information, I hope you have any ideas on how to solve it. Thanks!
Main.js (laravel-echo / socket.io connection):
import Echo from "laravel-echo"
window.io = require('socket.io-client')
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'https://example.com/socket.io',
auth: {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
}
});
Chat component (listener):
window.Echo.private(`chat.${this.user.id}`)
.listen('NewMessage', (e) => {
console.log(e.message);
});
Nginx config:
server {
server_name example.com www.example.com;
root /var/www/html/example/api/public/;
index index.html index.php;
location / {
root /var/www/html/example/web/dist/;
try_files $uri $uri/ /index.html;
}
location /socket.io {
proxy_pass http://localhost:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /api {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
}
}
Laravel-echo config:
{
"authHost": "https://example.com/api",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "xxx",
"key": "xxxxxxx"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
As it's working in local but not in production, you need to check if socket.io path on your domain is proxied to laravel echo server. To check this path run this path in browser and it must return an array. if it's not proxied you will be redirected according to laravel rules.
https://example.com/socket.io must return:
{"code":0,"message":"Transport unknown"}

Laravel echo server won't start

Laravel echo server keeps returning failed status on my server
Code
laravel-echo-server.json
{
"authHost": "http://example.com",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "311dr094tf98745ce",
"key": "fa43bffb3rth63f5ac9c386916ae28e6"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": false,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "http://localhost:80",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
echo-pm2.json
{
"name": "echo",
"script": "laravel-echo-server.json",
"args": "start"
}
then i run pm2 start echo-pm2.json and status is online but when i visit my web page i keep getting
GET https://www.example.com:6001/socket.io/?EIO=3&transport=polling&t=NA2SNHk net::ERR_TIMED_OUT
Any idea?
"authHost": "http://example.com"
I have this set to the url of my localhost, not sure if it helps you
Solved
The problem was my url was set to https (GET https://www.example.com) while my actual address is running on http (GET http://www.example.com)
so that extra S... :)

React Native laravel echo listen is not working

Everything works just fine with laravel redis and everything running and working with no issues. However, I am not getting a console log for my event from react native and there is no warnings or errors. Here is my code
App.js
import Echo from 'laravel-echo/dist/echo';
import Socketio from 'socket.io-client';
const echo = new Echo({
broadcaster: 'socket.io',
host: 'http://localhost:6001',
client: Socketio,
auth: {
headers: {
'Authorization': 'Bearer 7ebee00f74965d11cae413998a13c802',
},
},
});
let orderId = 9;
echo.channel('app_database_order.' + orderId)
.listen('.OrderStatusEvent', (e) => {
console.log('event: ', e);
});
and this is my Laravel echo server console
⚠ Starting server in DEV mode...
✔ Running at localhost on port 6001
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
Channel: app_database_order.9
Event: OrderStatusEvent
Channel: app_database_order.9
Event: OrderStatusEvent
Channel: app_database_order.9
Event: OrderStatusEvent
laravel-echo-server.json:
{
"authHost": "http://localhost",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "AppId",
"key": "7ebee00f74965d11cae413998a13c802"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port":"6379",
"host":"127.0.0.1",
"db":"0",
"password":"0TaEotyBIzOSym6zEXdsD2f2VCiPJmj4s/2nkGgyC4BBnm7CVUphOPCJ3LYoEUWuuqLokqJ/CCrJ3HqR"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
},
"publishPresence": true
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
And I am trying to trigger the event by calling this API from postman:
Route::post('/ship', function (Request $request)
{
$order_id = $request->input('order_id');
$order = Order::findOrFail($order_id);
event(new OrderStatus($order));
return true;
});
Order Status Event:
class OrderStatus implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $order_id;
public function __construct(Order $order)
{
$this->order_id = $order->id;
}
public function broadcastOn()
{
return new Channel('order.'.$this->order_id);
}
public function broadcastAs() {
return 'OrderStatusEvent';
}
}
I am using expo to run react-native, so the log should appear in the expo console,
and nothing appears.
I am working on this problem for 3 days with no solutions :(.
Please help.
Try downgrading to Echo version 2.4.0. It solved the problem for me.
Also, in place of echo.listen() use echo.on()

laravel-echo-server - Client can not be authenticated, got HTTP status 403 JWT auth

I am building a SPA. I am using laravel for backend and vuejs for front end. They are running independently on different port. (Using laravel api)
Laravel is running on 8000
Vue is running on 8082
I am allowing user to authenticate using JWT Auth.
I am trying to make a real time data transfer application using laravel-echo-server and have used public channel to transfer data and this worked great.
Now I need to use private channel to broadcast data to authenticated user and that's where I am not able to figure out how to make it work and I am getting error such as
NuG9yhe46E3N9Q6zAAAA could not be authenticated to private-some-p
rivate-channel
Client can not be authenticated, got HTTP status 403
here is laravel-echo-server.json file
{
"authHost": "http://localhost:8000",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "mprimeapp",
"key": "d8f6639653d0c4f31efa56d27f6ab502"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port": "6379",
"host": "127.0.0.1"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "localhost",
"port": "8890",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": ["http://localhost:8000", "http://localhost:8082"],
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
I am using redis not sqlite database that is running also.
My main.js file vue app
const tokenData = JSON.parse(window.localStorage.getItem('authUser'))
if (tokenData !== null) {
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'http://localhost:8890',
auth: {
headers: {
'Authorization': 'Bearer ' + tokenData.token
}
}
})
}
window.Echo.private('some-private-channel')
.listen('SomeEvent', (e) => {
console.log(e)
})
here are feew things that i wanted to ask.
authEndpoint in laravel-echo-server.json file, how does it work? do i have to make an end point in my api.php file to authenticate user.
clients array in laravel-echo-server.json file, what is use of data inside it?
In Laravel app inside config/app.php
I have this line uncommented App\Providers\BroadcastServiceProvider::class
In BroadcastServiceProvider.php
public function boot()
{
Broadcast::routes([ 'middleware' => [ 'auth:jwt.auth' ] ] ]);
require base_path('routes/channels.php');
}
UPDATE
It seems like there is something wrong with above function. To be more specific thing thing [ 'middleware' => [ 'api', 'jwt.auth' ] ]
Getting this error after changing middlware array
[15:58:13] - vAKK8BO7vs2TZy4SAAAA could not be authenticated to private-some-private-channel
{
"message": "Auth guard [jwt.auth] is not defined.",
"exception": "InvalidArgumentException",
"file": "C:\\xampp\\htdocs\\mipm-l\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\AuthManag
er.php",
.....
.....
.....
Have you installed and configured a JWT Auth package https://github.com/tymondesigns/jwt-auth ? (Sorry but you did not mention)
Also make sure you have following lines in your Kernel.php
protected $routeMiddleware = [
...
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
...
];

Resources