Laravel echo服务器无法在生产服务器中运行

z0m*_*ale 10 php redis socket.io laravel laravel-echo

我有socket.io作为一个广播员与laravel回声的问题.

我试过了什么:

php artisan cache:clear
php artisan config:clear
Run Code Online (Sandbox Code Playgroud)

我可以看到用户在日志中连接:

0|Socket-Connection  | [11:17:00 AM] - ********** joined channel: test-channel
0|Socket-Connection  | [11:17:01 AM] - ********** authenticated for: private-user.1
0|Socket-Connection  | [11:17:01 AM] - ********** joined channel: private-user.1
Run Code Online (Sandbox Code Playgroud)

我的队列正在运行,正在正确记录所有事件.

我可以在redis控制台中完美地看到redis我的事件和数据库通知.

但是没有广播事件,我没有在laravel-echo-server控制台中看到它们.一切都在我的本地主机上工作,但不是在制作中我失去了理智.

这是我的laravel echo JS:

if (typeof io !== 'undefined') {
    console.log(window.location.origin);
    window.Echo = new Echo({
        broadcaster: 'socket.io',
        host: window.location.origin + ':6001',
        auth: {
            headers: {
                Authorization: 'Bearer ' + bearerToken,
            },
        }
    });
    window.Echo.private('user.' + user_id).notification((notification) => {
        console.log(notification);
    });
}
Run Code Online (Sandbox Code Playgroud)

在我的用户模型上,我定义了这个:

/**
* @return string
*/
public function receivesBroadcastNotificationsOn()
{
    return 'user.' . $this->id;
}
Run Code Online (Sandbox Code Playgroud)

在我的频道中我有这个:

Broadcast::channel('user.{id}', function ($user, $id) {
    return (int)$user->id === (int)$id;
});
Run Code Online (Sandbox Code Playgroud)

这是我的echo服务器配置,其中所有路径都是正确的.我在我的localhost上测试了相同的文件,一切正常:

var echo = require('laravel-echo-server/dist');

echo.run({
    "appKey": "myappkey",
    "authHost": "https://url",
    "authEndpoint": "/broadcasting/auth",
    "database": "redis",
    "clients": [
        {
            "appId": "myappid",
            "key": "mykey"
        }
    ],
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "myhost",
            "password": "mysupersecretpassword"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "url",
    "port": "6001",
    "protocol": "https",
    "referrers": [],
    "sslCertPath": "/path/to/certificate.pem",
    "sslKeyPath": "/path/to/key",
    "verifyAuthPath": true,
    "verifyAuthServer": false
});
Run Code Online (Sandbox Code Playgroud)

我的redis日志在发布数据库通知时显示了这一点

1534840681.110359 [0 "IP ADDRESS HERE"] "PUBLISH" "private-user.2" "{\"event\":\"Illuminate\\\\Notifications\\\\Events\\\\BroadcastNotificationCreated\",\"data\":{\"title\":\"Ravim CONVULEX 50MG\\/ML  staatust muudeti\",\"notification_type\":\"element-soft-delete\",\"message\":\"Ravimi CONVULEX 50MG\\/ML  staatust muutis kasutaja Kalle \",\"url\":\"https:\\/\\/www.app.riskomed.com\\/admin\\/brands\\/all\",\"id\":\"30c37d0d-c39b-41bf-93fc-afa0c78ca9db\",\"type\":\"App\\\\Notifications\\\\API\\\\Management\\\\Medical\\\\Brands\\\\BrandSoftDeleteNotification\",\"socket\":null},\"socket\":null}"
Run Code Online (Sandbox Code Playgroud)

编辑

这是我的通知

        /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail', 'database', 'broadcast'];
    }
Run Code Online (Sandbox Code Playgroud)

小智 2

正如我看到您在生产服务器中使用“https”协议,因此您需要定义“sslCertPath”和“sslKeyPath”:

来自文档

使用 SSL 运行

  • 您的客户端实现必须从 https 访问 socket.io 客户端。
  • 服务器配置必须将服务器主机设置为使用 https。
  • 服务器配置应包含服务器上 ssl 证书和密钥的路径。