You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Svelte集成Pusher Presence Channel触发AuthError问题求助

排查Pusher Presence Channel认证错误(返回AuthError但接口200)

1. 响应缺少正确的Content-Type头

服务端返回JSON时未指定Content-Type: application/json,Pusher客户端可能无法正确解析响应内容,导致认证失败。

修改服务端代码,添加响应头:

export async function POST({request}) {
  const pusher = getPusher();
  const params = new URLSearchParams(await request.text());
  const socketId = params.get('socket_id');
  const user = {
    id: "1",
    user_info: {
      name: "User 1",
    },
  };
  const authResponse = pusher.authenticateUser(socketId, user);
  // 添加Content-Type头
  return new Response(JSON.stringify(authResponse), {
    headers: { 'Content-Type': 'application/json' }
  });
}

2. 调整用户数据结构以匹配Pusher要求

Pusher的authenticateUser方法接收的user对象,id为必填项,其余用户信息建议直接作为顶层属性(而非嵌套在user_info),避免客户端解析异常。

调整服务端user对象:

const user = {
  id: "1",
  name: "User 1" // 直接作为顶层属性
};

此时返回的user_data会是{"id":"1","name":"User 1"},更符合Pusher客户端的预期格式。

3. 验证socket_id是否正确获取

在服务端添加日志,确认从请求中拿到的socket_id不为空且与客户端的socket_id一致:

console.log('Received socket_id:', socketId); // 检查输出是否有效

如果socket_id为空或不正确,会导致Pusher生成的auth token无效。

4. 核对客户端与服务端的Pusher配置一致性

确保客户端初始化Pusher时的appKey、加密设置(encrypted选项)与服务端的配置完全一致:

  • 客户端示例(Svelte中):
import Pusher from 'pusher-js';

const pusher = new Pusher('YOUR_APP_KEY', {
  cluster: 'YOUR_CLUSTER',
  encrypted: true, // 需与服务端保持一致
  authEndpoint: '/pusher/auth'
});

如果服务端开启了加密而客户端未开启(或反之),会触发认证错误。

内容的提问来源于stack exchange,提问作者Gregable

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:17:32