提供基于HTTP的PubSub服务, 受到patchbay, smee.io和hookbot启发.
git clone https://github.com/BlackGlory/pubsub
cd pubsub
yarn install
yarn build
yarn bundle
yarn --silent startgit clone https://github.com/BlackGlory/pubsub
cd pubsub
yarn install
yarn docker:buildversion: '3.8'
services:
pubsub:
image: 'blackglory/pubsub'
restart: always
ports:
- '8080:8080'所有API中的namespace和channel都需要满足此正则表达式: ^.{0,255}$.
POST /namespaces/<namespace>/channels/<channel>
往特定频道发布消息, 所有订阅此频道的客户端都会收到消息.
发送JSON:
JSONValue
curl \
--header "Content-Type: application/json" \
--data "$payload" \
"http://localhost:8080/namespaces/$namespace/channels/$channel"fetch(`http://localhost:8080/namespaces/${namespace}/channels/${channel}`, {
method: 'POST'
, headers: {
'Content-Type': 'application/json'
}
, body: JSON.stringify(content)
})GET /namespaces/<namespace>/channels/<channel>
通过Server-Sent Events(SSE)订阅特定频道.
收到的每条JSON:
JSONValue
sse-cat "http://localhost:8080/namespaces/$namespace/channels/$channel"const es = new EventSource(
`http://localhost:8080/namespaces/${namespace}/channels/${channel}`
)
es.addEventListener('message', event => {
const payload = event.data
const content = JSON.parse(payload)
console.log(content)
})通过环境变量PUBSUB_HOST和PUBSUB_PORT决定服务器监听的地址和端口,
默认值为localhost和8080.
通过环境变量PUBSUB_SSE_HEARTBEAT_INTERVAL可以设置SSE心跳包的发送间隔, 单位为毫秒.
在默认情况下, 服务不会发送SSE心跳包,
此时半开连接的检测依赖于服务端和客户端的运行平台的TCP Keepalive配置.
当PUBSUB_SSE_HEARTBEAT_INTERVAL大于零时,
服务会通过SSE的heartbeat事件按指定间隔时间发送空白数据.
客户端若要实现半开连接检测, 则需要自行根据heartbeat事件设定计时器, 以判断连接是否正常.
- JavaScript/TypeScript(Node.js, Browser): https://github.com/BlackGlory/pubsub-js