Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kernel API for subscribe broadcast message by SSE #13694

Merged
merged 6 commits into from
Jan 1, 2025

Conversation

Zuoqiu-Yingyi
Copy link
Contributor

@Zuoqiu-Yingyi Zuoqiu-Yingyi commented Jan 1, 2025

REL: #9031

当前广播消息监听 API /ws/broadcast 每次仅能订阅一个信道

新增内核 API /es/broadcast/subscribe,用户可以使用 EventSource 同时订阅多个广播信道
The new kernel API /es/broadcast/subscribe allows users use EventSource to subscribe multiple broadcast channels at the same time

使用示例 | Examples:

// 订阅所有广播信道 | Subscribe to all broadcast channels
const es = new EventSource("/es/broadcast/subscribe");
es.addEventListener("channel-name", (e) => {
  console.log(e);
});

// 仅订阅广播信道 "ch1" | Subscribe to channel "ch1"
const es1 = new EventSource("/es/broadcast/subscribe?channel=ch1");
es1.addEventListener("ch1", (e) => {
  console.log(e);
});

// 同时订阅广播信道 "ch1" 与 "ch2" | Subscribe to channel "ch1" and "ch2"
const es12 = new EventSource("/es/broadcast/subscribe?channel=ch1&channel=ch2");
es12.addEventListener("ch1", (e) => {
  console.log(e);
});
es12.addEventListener("ch2", (e) => {
  console.log(e);
});

// 自定义连接断开后自动重连时间间隔 (单位: ms) | Custom automatic reconnection interval after disconnection (unit: ms)
const es_retry = new EventSource("/es/broadcast/subscribe?retry=1000");

数据格式 | Data format:

  • 字符串消息 | string message: abc -> abc
    • 原格式
      raw
  • 二进制消息 | binary message: abc -> "YWJj"
    • 使用双引号 " 包裹的 Base64 编码字符串
      A Base64-encoded string wrapped in quotation marks "

已测试 | Tested

@Zuoqiu-Yingyi Zuoqiu-Yingyi marked this pull request as ready for review January 1, 2025 05:20
@88250 88250 added this to the 3.1.19 milestone Jan 1, 2025
@88250 88250 merged commit 69c9539 into siyuan-note:dev Jan 1, 2025
3 checks passed
@88250
Copy link
Member

88250 commented Jan 1, 2025

感谢你的贡献,思源有你更精彩!
Thank you for your contribution. SiYuan will be more wonderful with you!

@Zuoqiu-Yingyi Zuoqiu-Yingyi deleted the feat/broadcast-subscribe branch January 1, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants