Laravel Echo Server 换为 Soketi

DEMO

lab.dogeow.com/chat

Laravel

Composer

推送需要用到

composer require pusher/pusher-php-server

广播中间件

routes/api.php 添加

use Illuminate\Support\Facades\Broadcast;
Broadcast::routes(['middleware' => ['auth:sanctum']]);

广播配置

configs/broadcasting.php 修改为

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY', 'app-key'),
            'secret' => env('PUSHER_APP_SECRET', 'app-secret'),
            'app_id' => env('PUSHER_APP_ID', 'app-id'),
            'options' => [
                'host' => env('PUSHER_HOST', '127.0.0.1'),
                'port' => env('PUSHER_PORT', 6001),
                'scheme' => env('PUSHER_SCHEME', 'http'),
                'encrypted' => true,
                'useTLS' => env('PUSHER_SCHEME') === 'https',
            ],
            'client_options' => [
                // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
            ],
        ],

.env 配置

BROADCAST_DRIVER=pusher

# WebSocket
PUSHER_APP_KEY=app-key
PUSHER_APP_ID=app-id
PUSHER_APP_SECRET=app-secret
PUSHER_HOST=127.0.0.1
PUSHER_PORT=6001
SOKETI_DEFAULT_APP_ENABLE_CLIENT_MESSAGES=true

KEY、ID 和 SECRET 自己改下

前端

yarn

yarn add pusher-js
yarn remove socket.io-client

配置

原本

window.io = require("socket.io-client");
window.Echo = new Echo({
  broadcaster: "socket.io",
  host: window.location.hostname + ":6001",
  auth: {
    headers: {
      Authorization: localStorage.token,
    },
  },
});

改为

window.Pusher = require("pusher-js");

window.Echo = new Echo({
  broadcaster: "pusher",
  key: process.env.REACT_APP_PUSHER_APP_KEY,
  wsHost: process.env.REACT_APP_PUSHER_HOST,
  wsPort: process.env.REACT_APP_PUSHER_PORT,
  wssPort: process.env.REACT_APP_PUSHER_PORT,
  forceTLS: false,
  encrypted: true,
  disableStats: true,
  enabledTransports: ["ws", "wss"],
  authorizer: (channel, options) => {
    return {
      authorize: (socketId, callback) => {
        axios
          .post("/broadcasting/auth", {
            socket_id: socketId,
            channel_name: channel.name,
          })
          .then((response) => {
            callback(false, response.data);
          })
          .catch((error) => {
            callback(true, error);
          });
      },
    };
  },
});

.env

# WebSocket
REACT_APP_PUSHER_APP_KEY=app-key
REACT_APP_PUSHER_HOST=127.0.0.1
REACT_APP_PUSHER_PORT=6001

感谢

博客:Soketi WebSockets 无法使用客户端消息

本作品采用《CC 协议》,转载必须注明作者和本文链接
无论在现实或是网络中,我都是孤独的。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 12

这个 Soketi 有什么优势吗 对比 workerman

1年前 评论
小李世界 (楼主) 1年前

就是连接不上,一直提示4001

1年前 评论
小李世界 (楼主) 1年前

我的是用 Docker 创建的,需要传值进去,应该还可以写 config.json 目前还没找到怎么传入。

docker run -p 6001:6001 -p 9601:9601 \
-e SOKETI_DEFAULT_APP_ID='app-id' \
-e SOKETI_DEFAULT_APP_KEY='app-key' \
-e SOKETI_DEFAULT_APP_SECRET='app-secret' \
quay.io/soketi/soketi:1.0-16-debian
1年前 评论
小李世界 (楼主) 1年前

连接是正常了,日志出现以下错误,请帮忙看看吧?

[2022-09-23 14:33:22] local.ERROR: Pusher error: cURL error 7: Failed to connect to 127.0.0.1 port 6001: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://127.0.0.1:6001/apps/624274/events?auth_key=app_key&auth_timestamp=1663914802&auth_version=1.0&body_md5=7d7566634482e81475ad65df76b4216e&auth_signature=624bfbb8d28312805ef9f6fe3b30741dcef9c7cd22606bba3180fc7e69f8602b. {"exception":"[object] (Illuminate\\Broadcasting\\BroadcastException(code: 0): Pusher error: cURL error 7: Failed to connect to 127.0.0.1 port 6001: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://127.0.0.1:6001/apps/624274/events?auth_key=5b329ffb56f&auth_timestamp=1663914802&auth_version=1.0&body_md5=7d7566630982e81475ad65df76b4216e&auth_signature=624bfbb8d28312805ef9f6fe3b30741dcef9c7cd22606bba3180fc7e69f8602b. at /var/www/laravel/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:164)
[stacktrace]
1年前 评论
小李世界 (楼主) 1年前
Johnson16 (作者) 1年前
小李世界 (楼主) 1年前
Johnson16 (作者) 1年前
小李世界 (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!