How to model Push Notifications on server

2019-04-15 12:40发布

问题:

Brief Description:

Well, since many days I've been looking for an answer to this question but there seems to be answers for 'How to create a Push Notification Server' and like questions. I am using node.js and it's quite easy to 'create' a push notification server using sock.js (I've heard socket.io isn't good as compared to sock.js). No problem till here. But what I want is how to model such a server.

Details:

OK, so, let's say I've an application where there's a chat service (just an example this is, actual thing is big as you might have guessed). A person sends a message in a room and all the people in the room get notified. But what I want is a 'stateful' chat - that is, I want to store the messages in a data store. Here's where the trouble comes. Storing the message in the database and later telling everyone that "Hey, there's a message for you". This seems easy when we need the real-time activity for just one part of the app. What to do when the whole app is based on real-time communication? Besides this, I also want to have a RESTful api.

My solution (with which I am not really happy)

What I thought of doing was this: (on the server side of course)

                 Data Store
                     ||
                 Data Layer (which talks to data store)
                     ||
            ------------------
            |                |
     Real-Time Server   Restful server

And here, the Real-time server listens to interesting events that the data-layer publishes. Whenever something interesting happens, the server notifies the client. But which client? - This is the problem with my method

Hope you can be of help. :)

UPDATE:

I think I forgot to emphasize an important part of my question. How to implement a pub-sub system? (NOTE: I don't want the actual code, I'll manage that myself; just how to go about doing it is where I need some help). The problem is that I get quite boggled when writing the code - what to do how (my confusion is quite apparent from this question itself). Could please provide some references to read or some advice as to how to begin with this thing?

回答1:

I am not sure if I understood you correctly; but I will summarize how I read it:

  1. We have a real-time chat server that uses socket connections to publish new messages to all connected clients.
  2. We have a database where we want to keep chat logs.
  3. We have also a restful interface to access the realtime server to get current chats in a lazier manner.

And you want to architect your system this way:

In the above diagram, the components I circled with purple curve wants to be updated like all other clients. Am I right? I don't know what you meant with "Data Layer" but I thought it is a daemon that will be writing to database and also interfacing database for other components.

In this architecture, everything is okay in the direction you meant. I mean DataStore is connected by servers to access data, maybe to query client credentials for authentication, maybe to read user preferences etc.

For your other expectation from these components, I mean to allow these components to be updated like connected clients, why don't you allow them to be clients, too?

Your realtime server is a server for clients; but it is also a client for data layer, or database server, if we prefer a more common naming. So we already know that there is nothing that stops a server from being a client. Then, why can't our database system and restful system also be clients? Connect them to realtime server the same way you connect browsers and other clients. Let them enjoy being one of the people. :)

I hope I did not understand everything completely wrong and this makes sense for the question.