zavrik/laravel-centrifugo
Composer 安装命令:
composer require zavrik/laravel-centrifugo
包简介
README 文档
README
This package is an easier way for use Centrifugo v3 with Laravel Framework and Docker. It realises proxy connections inside docker for trigger events when we have some data in WebSocket Connection.
Install
- Copy to .env
CENTRIFUGO_APP_DOCKER_URL=http://<YOUR_CENTRIFUGO_CONTAINER_NAME>:<YOUR_CENTRIFUGO_PORT>
CENTRIFUGO_PROXY_DOCKER_URL=http://<YOUR_CONTAINER_NAME_FOR_PROXY_REDIRECT>
CENTRIFUGO_TOKEN_HMAC_SECRET_KEY=37246e8e-6943-444f-9f5c-c8d3241af361
CENTRIFUGO_ADMIN_PASSWORD=c32c421a-2123-4e8b-b063-c849e6dab599
CENTRIFUGO_ADMIN_SECRET=d37f2351-24e6-4373-9462-c2030e0eaf40
CENTRIFUGO_API_KEY=719521ac-72ce-4621-ad36-5fe0c8cbfd47
- execute command
php artisan laravel:centrifugo:initand clear cache. - Add to your
docker-composefile this container
centrifugo:
container_name: centrifugo
image: centrifugo/centrifugo:v3
volumes:
- ./docker/centrifugo/config.json:/centrifugo/config.json
command: centrifugo -c config.json
ports:
- '8000:8000'
ulimits:
nofile:
soft: 65535
hard: 65535
networks:
- <YOUR DOCKER NETWORK>
Usage
Autogenerate config
Command php artisan laravel:centrifugo:init generate config/centrifugo.php for Laravel and docker/centrifugo/config.json for Centrifugo.
It Generates for you default proxy routes by .env and centrifugo keys.
Security
By Default, it uses Laravel Passport Token for identify users and possible to connect Centrifugo.
You can write your own Guard by implement AuthGuardInterface.
interface AuthGuardInterface
{
public function auth(Request $request): int;
public function encodeUserId(int $userId): string;
public function decodeUserId(string $hashedUserId): int;
}
auth(Request $request): int
Give you Request with body that you can send, and you must return userID by whom WebSocket was connected.
encodeUserId(int $userId): string
Encoding User ID in unique string for security display User ID.
By Default in use Hashids library.
decodeUserId(string $hashedUserId): int
Decoding Hashed User ID for correctly setUp User ID in events.
Proxy Events
Centrifugo has 4 proxy routes. We use only 3 of them because of refresh proxy unused by AuthGuard Authentication. You can select which of Users can connect to Centrifugo by Guarding You Application Token.
connect
After Successfully Guarded Your token it dispatches ConnectEvent with UserID.
subscribe
Dispatch SubscribeEvent with UsedID and Channel Name after User successfully subscribed on Channel
publish
Dispatch PublishEvent with UserID, Channel and Published Data after User successfully published data in Channel
FrontEnd usage
You must use default Centrifugo JS library. Example with default BearerToken Connection:
const centrifuge = new Centrifuge("ws://localhost:8000/connection/websocket");
centrifuge.setConnectData({
token: bearerToken
});
centrifuge.connect();
let subscription = centrifuge.subscribe(channel, function(ctx) {
container.innerHTML = ctx.data.value;
document.title = ctx.data.value;
});
By centrifuge.setConnectData you can send Any data to you Request Body in AuthGuard.
zavrik/laravel-centrifugo 适用场景与选型建议
zavrik/laravel-centrifugo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.05k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zavrik/laravel-centrifugo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zavrik/laravel-centrifugo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-05