Merc
Merc

Reputation: 17067

Real time chat app using Polymer and node

I have a node application using Polymer as front-end. The app runs behind an nginx reverse proxy, which deals with HTTPs etc using HTTP2. I am using cookie sessions with signed cookies.

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.

My first port of call was Primus. But there are things I just don't quite get:

Is there a standard solution to all of this that I am not aware of?

Upvotes: 0

Views: 404

Answers (1)

Hunex
Hunex

Reputation: 655

I worked recently on a chat project and I also used Polymer on the client side.

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. 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.

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 ...

It is really up to you how you design your Feathers app. As I understand you want something like in Facebook, where you have these tabs with different people or multiple people.

For this, I used a Master-detail data structure:

Conversation (1) --- (n) Message

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

{
  "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

{
  "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).

Upvotes: 0

Related Questions