【问题标题】:Pusher ChatKit - rooms and roomSubscriptions - how to detect new messagesPusher ChatKit - 房间和房间订阅 - 如何检测新消息
【发布时间】:2018-11-16 21:52:21
【问题描述】:

最近我在我的 Vue 项目中实现了Pusher ChatKit

我已经查看了js documentation,但我对启动应用程序的流程感到困惑,因此我有新消息的活跃听众。

我有 5 个房间,currentUser 是其中的一部分。

当我这样做时

chatManager
    .connect()
    .then(currentUser => {
    })

我得到了当前用户,我可以访问他的roomsroomSubscriptions

1) 有什么区别?用户所在的任何房间也应该是他订阅的房间,不是吗?

文档说我需要订阅每个用户房间才能设置onNewMessage() 挂钩。

所以我是这样做的:

chatManager
    .connect()
    .then(currentUser => {
      this.initiateNewChatState(currentUser)
    })

initiateNewChatState(currentUser){
    for(let room of currentUser.rooms){
      currentUser.subscribeToRoom({
         roomId: room.id,
         hooks: {
         onNewMessage: message => {
          console.log(`Received new message ${message.text}`)
          this.$store.commit('CHATKIT_message', message)
         }
       },
       messageLimit: 10
     })
  }
}

2) 但是现在当我只收到一个房间的新消息时,每个房间钩子都会触发钩子(5 次)。

现在,一旦我完成了订阅循环,我下次运行 chatManager.connect() 时就不需要这样做了,因为 currentUser.roomSubscriptions 已经填满了我上次订阅的房间,甚至在 this.initiateNewChatState() 之前调用。

3) 这就引出了一个问题,订阅用户已经加入的用户房间的正确流程是什么,以及如何检测新消息和新房间创建(当有人开始与您聊天时)?

文档非常简化,无法在实际案例中实施。有人有这方面的经验吗?

谢谢!

【问题讨论】:

  • 嗨,我几天前在 React Native 应用程序中也是这样做的。不幸的是,这是我发现这样做的唯一方法。如果你找到更好的方法我也想知道:)

标签: vue.js chat pusher chatkit


【解决方案1】:

不幸的是,这是目前唯一的方法。在 Pusher ChatKit 中,每个操作都取决于当前用户和用户对房间的订阅。用户不一定订阅他/她所属的房间。 它既有优点也有缺点。 但现在我可以说,你可以设置 messageLimit: 0 。当您第一次加载页面时,这不会获取所有消息。 而是在用户单击房间以查看消息时使用以下方法获取消息:

           currentUser.subscribeToRoom({
                roomId: room.id,
                hooks: {
                    onNewMessage: (message) => {
                        // do something with the message
                    },
                    onUserStartedTyping : (user) => {
                        // do something with the user
                    },
                    onUserStoppedTyping : (user) => {
                        // do something with the user
                    },
                },
                messageLimit: 10
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2012-08-30
    相关资源
    最近更新 更多