Mongoose 查询已连接的朋友 [英] Mongoose query for connected friends
问题描述
如何使用 mongoose 在我的好友列表中有效地显示已连接用户"状态?
我也在使用 socket io(我打算切换到 socket p2p)
我的好友列表是一个以 uid 、 name 、 pic 、 socket 作为道具的对象数组,这个数组存储在我的个人资料文档中.
我在这里看到了几个选项,但不知道什么是最好的
使用运算符 $in 查询所有连接到应用程序的用户,并将结果与我的朋友数组进行匹配
将每个登录的用户存储在一个单独的集合中并进行某种比较
在 socket io 适配器中搜索每个朋友的 socket id(这里需要处理几个问题,例如:浏览器刷新)
感谢您的帮助
考虑使用 Primus 和
Primus Rooms 添加了一项功能,让客户(在您的情况下是朋友)加入某个房间",然后向所有连接的客户广播消息.
由于这些开源工具可以为您提供所需的功能,因此重新发明轮子是没有意义的.
关于猫鼬:Primus Rooms 默认使用内存适配器".这意味着当客户加入房间时它不持久化.但是,您可以随意为 Primus Rooms 编写 Mongoose 适配器并将其贡献给社区.p>
how can i effecively display the status 'connected user' on my Friends list using mongoose ?
am using socket io too (and i plan to switch to socket p2p)
my friend list is an array of objects with uid , name , pic , socket as props and this array is stored in my profile document.
i see a couple of options here but can't figure out what's best
use the operator $in to query all the users connected to the app and match the result with my array of friends
store every logged in user in a separate collection and make a sort of comparaison
search in socket io adapter with socket id of each friend ( couple of issues to handle here ex: browser refresh)
thanks for your help
Consider using Primus and Primus Rooms for this purpose. Basically Primus is an abstraction for your socket layer and gives you the possibility to use different web socket implementations.
Primus Rooms adds the functionality to let clients (friends in your case) join a certain 'room' and then broadcast messages to all connected clients.
As these open source tools could give you the wanted functionality it makes no sense to reinvent the wheel.
Regarding Mongoose: Primus Rooms uses a "memory adapter" by default. That means that it is not persisted when clients join rooms. However, feel free to write a Mongoose Adapter for Primus Rooms and contribute it to the community.
这篇关于Mongoose 查询已连接的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!