Centrifugo language-independent real-time messaging service

Centrifugo language-independent real-time news service, based on golang write, provides websocket and sockjs compatible process, the use of very simple
but also supports extended redis, the following is a simple test run

Preparing the Environment

  • docker-compose documents
 
version: "3"
services: 
  centrifugal:
    image: Centrifugal / Centrifugal
    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
  repeat:
    Image: repeat
    ports: 
    - "6379:6379"
 
  • Profiles

    Mainly on the parameters, run the test using the actual need to be adjusted

{
  "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 integration

It contains jwt generation and simple and based on the official nodejs sdk demo (integrated 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();
 

&& test run

  • Start Service
docker-compose up -d
  • Start nodejs demo application
cd web
yarn 
yarn s
  • Dissemination of information through 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"
        }
    }
}'
 
  • effect

 

  • redis key

 

Explanation

Centrifugo 还是比较方便的,使用起来也比较简单,实际上类似的工具还是很多的,nchan。。。 都挺不错的,同时官方文档还是不错的
内容比较详细

参考资料

https://github.com/centrifugal/centrifugo
https://nchan.io/
https://github.com/rongfengliang/centrifugo-docker-compose

Guess you like

Origin www.cnblogs.com/rongfengliang/p/11367765.html
Recommended