所以可能是一个非常基本的问题,但我想要实现的是一个具有实时功能的 Web 应用程序,它使用 socket.io 和带有可尾游标的 mongodb 封顶集合(普通的 mongodb 集合也用于持久数据)。
现在让我们假设该应用程序是一个在线聊天系统,其中包含房间、多个用户和飞来飞去的消息。
两个主要问题如下:
a) When the first initially joins the app, how/where should the initial set of data (list of chat rooms, maybe a count of users next to them) be loaded from...
-> persistent mongo storage : db.Rooms.find().orderBy({ date : -1 })
-> retrieved from "memory", a capped collection, some form of stream....
b) When the second user joins, should their initial set of data be loaded from
-> either of the first two options for (a) or alternatively or hit the first client up for their "live" version of data
紧随其后的是,当第一批用户加入一个有人聊天的房间时,他们需要将历史记录提高到一个点……而不是实时数据……也许看看旧消息……等等等等。
基本上,目前我正在创建 socket.io 房间,我根据用户所在的“主干”视图订阅用户,并通过 socket.io 将“实时”数据直接加载给他们。
我主要对初始数据加载感到困惑,我猜想正确的架构客户端 -> 消息队列 -> db -> 消息队列 -> 客户端类型的设置
下面的伪代码排序
Initialize App and Sockets
==== assumption: application is called chat, has chat rooms, messages flying about in them ====
User 1 socket connect -> join "chat"
sockets requests room list (top X rooms) -> mongo db query?
User 2 socket connect -> join "chat"
socket requests room list (top X rooms) -> mongo db query? memcache? tailable cursor?
==== messages exist and are continually being sent to chatRoom1 =====
Users 1 joins "chatRoom1" -> socket notifies others a new user has joined
socket requests user list and top X messages and Y users
User 2 joins "chatRoom1" -> socket notifies others a new user has joined
socket requests user list and top X messages and Y users (potentially newer messages that user 1)
New Messages follow this process
// client event handler on a new message, write to div for the correct room
Client -> socket.on("new message", function(data) { $("#" + data.room).find("div.messages").append(data.message) }
// client sending a new message, specifies room name and message
Client -> socket.emit("new message", { "room" : "chatRoom1", "message" : "contents of the message" }
// Server receives message and does two things
// a) sends it to other clients of the same room
Server -> socket.on("new message", function(data) { io.sockets.in(data.room).emit("new mesage", data) } )
// b) stores it in the collection (capped?)
谢谢!