简体   繁体   中英

Store socket created by socket.io in MongoDB

I am trying to create an application based on Node.js, using the Express, Socket.io and MongoDB (by mongoose / node mongo native driver).

My problem is that, along with establishing a connection to the client by WebSockets also I get additional informations that the storage and processing will be the best within database. Then with the arrival of the events I am looking for client on the basis of additional information and by sockets I send notifications to those clients.

My question - is there a possibility to store the objects created with the socket connection to the database?

If not, my idea is to store them in an array together with an additional variable that will allow them to find through found items in the database for additional information. Am I right?

client --> create conection --> send data --> server --> search DB for matching data --> match sockets to founded data --> send notifactions by founded sockets

Sincerely

对于任何通过Google遇到此问题的人来说,似乎现在有一个socket.io-mongo模块来处理这个问题。

you could keep a list of sockets and what node process they belong to in mongodb and then message that process when a message needs to be delivered to a specific socket. Something like each message having

{delivery_to:all} or {delivery_to:[1, 2, 3, 4]} where the number is a socket id.

But the elegant way is to use a capped collection in mongodb and have all processes listen to the collection and handle any messages belonging to that process. A good description of a queue implementation using mongodb can be found here.

http://www.10gen.com/presentations/mongosv-2011/mongodb-as-a-message-queue

Ok,i dont know if it is best idea for message queue but as you mention mongoose. From client you can have http request with the message and room id (any unique random id and must join with initial connection to server or should register to server ir simply create a room) to server, and in server save the message to mongodb using mongoose, now using mongoose schema post method which get fired after mongoose do save emit the message to the room. So it does pub sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM