所以可能是一个非常基本的问题,但是我想要实现的是一个具有实时功能的Web应用程序,使用socket.io和带有tailable游标的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?)
谢谢!