Centrifugo 语言无关的实时消息服务
Centrifugo 语言无关的实时消息服务,基于golang编写,提供了websocket 以及sockjs 的兼容处理,使用上很简单
同时也支持基于redis的扩展,以下是一个简单的运行测试
环境准备
- docker-compose 文件
version: "3"
services:
centrifugal:
image: centrifugo/centrifugo
command: centrifugo -c config.json --engine=redis --redis_host=redis --redis_port=6379
ports:
- "8000:8000"
volumes:
- "./config:/centrifugo"
ulimits:
nproc: 65536
nofile:
soft: 65536
hard: 65536
redis:
image: redis
ports:
- "6379:6379"
- 配置文件
主要是关于运行时的参数,测试使用,实际需要调整
{
"secret": "05f0842d-c302-4036-a19f-6ac263b9f620",
"admin_password": "ca0e58bb-5fde-43b6-adce-b62392420ffc",
"admin_secret": "b10b2ab3-8e29-428b-85cb-42a32ba6ea57",
"api_key": "cbf46e80-3e00-4642-8f3a-369b8707304d",
"anonymous": true,
"publish": true,
"subscribe_to_publish": true,
"presence": true,
"debug":true,
"client_anonymous":true,
"join_leave": true,
"history_size": 10,
"history_lifetime": 300,
"history_recover": true,
"prometheus": true
}
nodejs web 集成
包含了jwt 生成以及以及简单的基于官方nodejs sdk 的demo(集成sockjs)
- package.json
{
"name": "web",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"centrifuge": "^2.2.1",
"jsonwebtoken": "^8.5.1",
"sockjs-client": "^1.3.0"
},
"scripts": {
"s":"node app.js"
}
}
- app.js
var Centrifuge = require("centrifuge")
var SockJS = require('sockjs-client');
var jwt = require('jsonwebtoken');
var token = jwt.sign({ sub: 'dalongdemo'}, '05f0842d-c302-4036-a19f-6ac263b9f620');
var centrifuge = new Centrifuge("http://localhost:8000/connection/sockjs", {
sockjs: SockJS
})
centrifuge.setToken(token)
centrifuge.subscribe("news", function(message) {
console.log(message);
});
centrifuge.connect();
运行&&测试
- 启动服务
docker-compose up -d
- 启动nodejs demo 应用
cd web
yarn
yarn s
- 通过api 发布消息
curl -X POST \
http://localhost:8000/api \
-H 'Authorization: apikey cbf46e80-3e00-4642-8f3a-369b8707304d' \
-d '{
"method": "publish",
"params": {
"channel": "news",
"data": {
"text": "dalongrong"
}
}
}'
- 效果
- redis key
说明
Centrifugo 还是比较方便的,使用起来也比较简单,实际上类似的工具还是很多的,nchan。。。 都挺不错的,同时官方文档还是不错的
内容比较详细
参考资料
https://github.com/centrifugal/centrifugo
https://nchan.io/
https://github.com/rongfengliang/centrifugo-docker-compose
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2018-08-17 harbor rest api 转graphql api
2018-08-17 ringojs java jar 集成使用
2018-08-17 ringojs 使用rp 包管理web 应用依赖
2018-08-17 ringojs 的包管理
2017-08-17 keycloak && docker安装 &&spring boot 集成使用
2017-08-17 gogs docker 安装
2016-08-17 强弱主机模式