2

简要说明

好吧,很多天以来,我一直在寻找这个问题的答案,但似乎有关于“如何创建推送通知服务器”和类似问题的答案。我正在使用 node.js,使用 sock.js 很容易“创建”一个推送通知服务器(我听说 socket.io 不如 sock.js)。到这里都没问题。但我想要的是如何为这样的服务器建模。

详情

好的,所以,假设我有一个应用程序,其中有一个聊天服务(这只是一个例子,实际的东西很大,你可能已经猜到了)。一个人在房间里发送消息,房间里的所有人都会收到通知。但我想要的是“有状态”聊天——也就是说,我想将消息存储在数据存储中。麻烦就在这里。将消息存储在数据库中,然后告诉所有人“嘿,有一条消息给你”。当我们只需要应用程序的一部分的实时活动时,这似乎很容易。当整个应用都是基于实时通信的时候怎么办?除此之外,我还想要一个 RESTful api。

我的解决方案(我对此并不满意)

我想做的是:(当然在服务器端)

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

在这里,实时服务器监听数据层发布的有趣事件。每当发生有趣的事情时,服务器都会通知客户端。但是哪个客户?- 这是我的方法的问题

希望你能有所帮助。:)

更新

我想我忘了强调我问题的一个重要部分。如何实现发布订阅系统?(注意:我不想要实际的代码,我会自己管理;我需要帮助的是如何去做)。问题是我在编写代码时非常困惑 - 该怎么做(我的困惑从这个问题本身就很明显)。请提供一些参考资料或一些关于如何开始这件事的建议?

4

1 回答 1

10

我不确定我是否理解正确;但我会总结一下我是如何阅读它的:

  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:

Current architecture of system

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?

Suggested design for backend components' connections to real-time server.

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.

于 2012-07-01T15:09:56.183 回答