Mongoose query for connected friends

2019-08-14 20:27发布

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

  1. use the operator $in to query all the users connected to the app and match the result with my array of friends

  2. store every logged in user in a separate collection and make a sort of comparaison

  3. search in socket io adapter with socket id of each friend ( couple of issues to handle here ex: browser refresh)

thanks for your help

1条回答
我命由我不由天
2楼-- · 2019-08-14 20:59

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.

查看更多
登录 后发表回答