【问题标题】:Retrieve the conversations with latest mesaage, node js, socket io, mysql chat application检索与最新消息、node js、socket io、mysql 聊天应用程序的对话
【发布时间】:2019-03-09 13:39:21
【问题描述】:

我在构建聊天应用程序方面相对较新。 Ionic、node js、socket io、mysql stack 用于为一对一和群聊构建聊天应用程序。对应的 db 架构中的表如下所示

会话及其 id 在此设计中被视为线程。

当发件人在组中插入新消息时: 表 'messenger_threads' 填充有新线程 id(如果是组消息,则为组 id)和主题(如果该组 id 条目是新的),否则不插入任何内容。 当发件人在私聊中插入新消息时: 线程 id 由客户端使用相应的发送方-接收方对生成。表 'messenger_threads' 填充了生成的线程 ID。如果同一个发送者接收者对的会话在表中,则不进行插入。

同样,在“messenger_participants”表中填充了相同 thread_id(保护 id)的群聊和私聊参与者

在表'messenger_messages'中,插入新消息,带有thread_id,消息体和发件人id(即表中的user_id) 因此,在特定的对话/线程中(群组或私人聊天) 所有消息和发件人详细信息均可用。

在'user_messages'表中,更新了消息id和相关用户的详细信息(发送者和接收者),以便更新每个用户的消息清除操作。

从表'users'表中检索用户ID详细信息

用户 属性 1.id 2.用户名 3.other_details..

能否请您协助我查询检索单个用户的所有对话,并使用 facebook messenger 等最新消息信息。同时在数据库设计中给我建议。

【问题讨论】:

    标签: mysql node.js sockets ionic-framework chat


    【解决方案1】:
    select mthread.subject subject,message.last_message 
    from messenger_threads mthread,
        (select mm.thread_id thread_id,mm.body last_message,max(mm.updated_at) max_date 
                from messenger_messages mm 
                    group by mm.thread_id) message
    where mthread.id in 
        (select thread_id from messenger_participants mpart where user_id in(?)) 
        and message.thread_id=mthread.id 
    order by mthread.updated_at desc;
    

    此查询应满足您的需要,前提是可以对其应用性能调整以进一步改进它。欢迎提出改进此查询的建议。

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 2014-03-08
      • 2020-01-18
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多