@ apiner/ 客户端
JavaScript 客户端 SDK 用于 定点符 ——网络应用实时WebSocket消息.
特征
- 公共、私人、存在和缓存渠道
- 自动与指数备份重接
- 私人/现场频道的客户事件
- 成员追踪
- TypeScript - 第一种带有完整类型定义
- 零依赖性 — 在任何现代浏览器中工作
- 无害环境管理、共同JS和脚本标签构建
安装
npm install @apinator/client
没有捆绑
另存为 index.html 打开它 脚本标签定义全局 Apinator数字 :
<!DOCTYPE html>
<html>
<body>
<script src="https://cdn.jsdelivr.net/npm/@apinator/[email protected]/dist/apinator.min.js"></script>
<script>
const client = new Apinator({
appKey: 'your-app-key',
cluster: 'eu',
});
client.connect();
const channel = client.subscribe('my-channel');
channel.bind('my-event', (data) => {
console.log('Received:', data);
});
</script>
</body>
</html>
然后发表 my-event 打开 my-channel 从您的服务器(例如:
@apinator/server)并登录在浏览器控制台上.
喜欢ES模块吗? 从模块中导入 :
<script type="module">
import { Apinator } from 'https://cdn.jsdelivr.net/npm/@apinator/[email protected]/dist/index.mjs';
</script>
节点.js
22+节点有一个全球 WebSocket在旧版本上,安装 ws
并传入:
import WebSocket from 'ws';
import { Apinator } from '@apinator/client';
const client = new Apinator({ appKey: 'your-app-key', cluster: 'eu', WebSocket });
没有一个人, connect() 日志 No WebSocket implementation found…,报告给
bind('realtime:error', ...) 移动到 unavailable 而不是重试.
快速启动
import { Apinator } from '@apinator/client';
const client = new Apinator({
appKey: 'your-app-key',
cluster: 'eu',
});
client.connect();
const channel = client.subscribe('my-channel');
channel.bind('my-event', (data) => {
console.log('Received:', data);
});
频道类型
公共频道
不需要认证 。 任何客户端都可以订阅.
const channel = client.subscribe('news');
channel.bind('update', (data) => { /* ... */ });
私人频道
需要服务器侧认证 。 前缀为 private-。 。 。 .
const client = new Apinator({
appKey: 'your-app-key',
cluster: 'eu',
authEndpoint: '/api/realtime/auth',
});
const channel = client.subscribe('private-orders');
channel.bind('new-order', (data) => { /* ... */ });
存在频道
像私人频道,但也追踪谁被订阅。 前缀为 presence-。 。 。 .
const presence = client.subscribe('presence-chat') as PresenceChannel;
presence.bind('realtime:subscription_succeeded', () => {
console.log('Members:', presence.getMembers());
console.log('Me:', presence.me);
});
presence.bind('realtime:member_added', (member) => {
console.log('Joined:', member);
});
presence.bind('realtime:member_removed', (member) => {
console.log('Left:', member);
});
channel.me 来源于已签名的 channel_data 由您返回 authEndpoint。 。 。 .
只有在 realtime:subscription_succeeded。 。 。 .
缓存通道
名为频道 cache-, (中文(简体) ), private-cache- 或者说 presence-cache- 保留其最后一个事件
30分钟。 订户迟到的客户端会收到 realtime:subscription_succeeded, (中文(简体) ),
紧接着紧紧地绑住你的手 subscribe()。 。 。 .
const job = client.subscribe(`private-cache-job-${jobId}`);
job.bind('job-finished', (data) => { /* runs even if the job finished before you subscribed */ });
如果没有缓存( 或过期) 事件没有到达, 所以保持一个倒置, 如状态 API电话.
客户事件
直接从客户端从私人或存在频道触发的事件 :
const privateChannel = client.subscribe('private-chat');
privateChannel.trigger('client-typing', { user: 'alice' });
连接国
监测连接寿命周期:
client.bind('state_change', ({ previous, current }) => {
console.log(`${previous} -> ${current}`);
});
国家: initialized → connecting → connected → disconnected 编号 : unavailable
API 参考
见 文件/参考文献 为全API.
浏览器支持
任何带有 WebSocket 的浏览器 fetch 支持:
- 色调 42+
- Firefox 39+ (英语)
- 萨法里10.1+
- 边缘14+
- 勇敢 10 + 号
链接
许可证
麻省理工学院——见 自由主义。 。 。 .