简体   繁体   English

使用Polymer和Node的实时聊天应用

[英]Real time chat app using Polymer and node

I have a node application using Polymer as front-end. 我有一个使用Polymer作为前端的节点应用程序。 The app runs behind an nginx reverse proxy, which deals with HTTPs etc using HTTP2. 该应用程序在Nginx反向代理后面运行,该代理使用HTTP2处理HTTP等。 I am using cookie sessions with signed cookies. 我正在使用带有已签名cookie的cookie会话。

I now need to add a "real time chat" to the app; 我现在需要向该应用添加“实时聊天”; I thought that in 2016 it would be easy... boy was I wrong. 我以为在2016年这很容易...男孩,我错了。

My first port of call was Primus. 我的第一个停靠港是Primus。 But there are things I just don't quite get: 但是有些事情我只是不太了解:

  • What happens if the node server is restarted? 如果重新启动节点服务器会怎样? Will all of the client need to reconnect? 所有客户端都需要重新连接吗?
  • The clients can 'register' to specific event types (which are then supposed to receive via Primus/Websockets/etc.) So, each opened "tab" will need its own ID; 客户端可以“注册”到特定的事件类型(然后应该通过Primus / Websockets / etc接收)。因此,每个打开的“选项卡”都需要有自己的ID。 if the server is restarted, and the client reconnect, can the server reuse the list of "subscriptions"? 如果服务器重新启动,并且客户端重新连接,服务器可以重用“订阅”列表吗? (this requires the client to have the same "id") (这要求客户端具有相同的“ id”)

Is there a standard solution to all of this that I am not aware of? 对于我不知道的所有这些,是否有标准解决方案?

I worked recently on a chat project and I also used Polymer on the client side. 我最近参与了一个聊天项目,并在客户端使用了Polymer。

On the server side you could use Feathers like I did. 在服务器端,您可以像我一样使用羽毛 Basically Feathers is a minimalist wrapper over Express and uses Websockets and Socket.IO under the hood for the realtime communication. 基本上,Feathers是Express的简约包装,并在内部使用Websockets和Socket.IO进行实时通信。 It works really well, you don't have to worry about creating connections and all that. 它确实运行良好,您不必担心创建连接以及所有其他事情。 They also have a client side JS library which you can easily wrap in a Polymer component. 它们还有一个客户端JS库,您可以轻松地将其包装到Polymer组件中。

What happens if the node server is restarted? 如果重新启动节点服务器会怎样? Will all of the client need to reconnect? 所有客户端都需要重新连接吗?

The answer is yes, they will reconnect automatically. 答案是肯定的,它们将自动重新连接。

The clients can 'register' to specific event types (which are then supposed to receive via Primus/Websockets/etc.) So, each opened "tab" will need its own ID ... 客户端可以“注册”到特定的事件类型(然后应该通过Primus / Websockets / etc接收)。因此,每个打开的“选项卡”都需要有自己的ID。

It is really up to you how you design your Feathers app. 如何设计Feathers应用程序真的取决于您。 As I understand you want something like in Facebook, where you have these tabs with different people or multiple people. 据我了解,您希望在Facebook中拥有类似的标签,这些标签中包含不同的人或多个人。

For this, I used a Master-detail data structure: 为此,我使用了主从数据结构:

Conversation (1) --- (n) Message 对话(1)-(n)消息

Example: 例:

Conversation 会话

{
  "doc_created_at": "2016-09-21T07:30:02.289Z",
  "doc_created_by": "299009a4-5423-4cdd-9e1a-59fca59404ae",
  "doc_id": "00f61c96-4bc6-4c46-a22d-de246314695c",
  "doc_patched_at": "2016-10-27T11:35:53.599Z",
  "doc_type": "conversation",
  "participants": [
    {
      "id": "635b05bc-ae23-4c5d-9ee5-87e7da2cac15",
      "name": "User 1"
    },
    {
      "id": "299009a4-5423-4cdd-9e1a-59fca59404ae",
      "name": "User 2"
    }
  ],
  "sender": {
    "id": "299009a4-5423-4cdd-9e1a-59fca59404ae",
    "name": "User 2"
  },
  "last_message": "How are you?"
}

Message #1 讯息1

{
  "conversation_id": "00f61c96-4bc6-4c46-a22d-de246314695c",
  "doc_created_at": "2016-09-23T06:10:28.727Z",
  "doc_created_by": "299009a4-5423-4cdd-9e1a-59fca59404ae",
  "doc_id": "00e5b904-c9fa-46f1-b108-9fc9a15d11fc",
  "doc_type": "message",
  "participants": [
    {
      "id": "635b05bc-ae23-4c5d-9ee5-87e7da2cac15",
      "name": "User 1"
    },
    {
      "id": "299009a4-5423-4cdd-9e1a-59fca59404ae",
      "name": "User 2"
    }
  ],
  "sender": {
    "id": "299009a4-5423-4cdd-9e1a-59fca59404ae",
    "name": "User 2"
  },
  "message": "Hi"
}

Message #2 讯息2

{
  "conversation_id": "00f61c96-4bc6-4c46-a22d-de246314695c",
  "doc_created_at": "2016-09-21T07:32:08.312Z",
  "doc_created_by": "299009a4-5423-4cdd-9e1a-59fca59404ae",
  "doc_id": "2a6c2f91-04a8-4447-a0a6-4b229d523afc",
  "doc_type": "message",
  "participants": [
    {
      "id": "635b05bc-ae23-4c5d-9ee5-87e7da2cac15",
      "name": "User 1"
    },
    {
      "id": "299009a4-5423-4cdd-9e1a-59fca59404ae",
      "name": "User 2"
    }
  ],
  "sender": {
    "id": "299009a4-5423-4cdd-9e1a-59fca59404ae",
    "name": "User 2"
  },
  "message": "How are you?"
}

I stored these information in a database ( Couchbase ). 我将这些信息存储在数据库( Couchbase )中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM