Saving data from node.js and socket.io

2,464 views
Skip to first unread message

Ericko

unread,
Jun 28, 2012, 2:38:59 AM6/28/12
to mongoo...@googlegroups.com
Hi,

I am trying to save the data through the socket.io, which will be connected to a html page. So every time the callback happen, I would like to save the data.
Here is the code:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ResultSchema = new Schema({
axis : String,
range: Number,
time : Number,
date : {type: Date, default: Date.now}
});

io.sockets.on('connection', function (socket) {
  console.log("browser connected");  
  csock = socket;
  //wait data from client, and send it to the browser
  gsock.on("json", function(data) {
     try {
        json = JSON.parse(data);
        socket.emit("data", json);
        
     } catch (Err) {
        console.log("skipping: " + Err);
        return; // continue
     }  
  });
  //wait for response from the web browser
  socket.on("result",function(data){
resultJson = JSON.parse(data);
        mongoose.connect('mongodb://localhost/database');
        mongoose.model('theResult', ResultSchema);
        var Result = mongoose.model('theResult');
        var result = new Result();
result.axis = resultJson.axis;
result.range = resultJson.range;
result.time = resultJson.time;
result.save(function(err){
if(err)
throw err;
else
console.log("saved!");
// mongoose.disconnect();
});
socket.emit("result",resultJson);
  });
});

It seems that it didn't even connect to the mongodb, there wasn't any output like "connection accepted" in the mongod console.

I have tried to put
mongoose.connect('mongodb://localhost/database');
mongoose.model('theResult', ResultSchema);
var Result = mongoose.model('theResult');
var result = new Result();
outside the socket.io code, it could connect, but still it didn't save any data.
I also have tried not to close it after saving, also didn't work.

Can mongodb work with socket.io? Is there anything that I miss?

My objective is I will get the data from the web page, save it into database, and I will have another socket io connection, get the data from the database, and send it to another web page. Is this possible to do? As in, can I access the mongodb simultaneously (writing and reading), from 2 socket.io connection but on the same node.js server. 

Thank you for any help. 

Victor Powell

unread,
Jun 28, 2012, 11:03:33 AM6/28/12
to mongoo...@googlegroups.com
well, you only want to call mongoose.connect once. Also, don't call mongoose.connect before you setup your models. If you never notice mongoose connecting with the first example you posted then thats a good sign your not using socket.io correctly. 

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Victor Powell

Ericko

unread,
Jun 28, 2012, 9:52:20 PM6/28/12
to mongoo...@googlegroups.com
Thanks for your reply.

Since I only need to call it once, so it is okay not to close the connection every time I save a new data right?

The problem with my first example is, I can get the result from the last code 
socket.emit("result",resultJson);

So doesn't it show that the socket.io is working fine? it's like it just skip all the mongoose section.
However, I am not experienced in using all of this(node.js,socket.io,mongoose), actually this is the first time I tried it.
So if it is possible, could you give a hint of what's wrong with it?

Thank you so much.

For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en

Victor Powell

unread,
Jun 28, 2012, 10:02:41 PM6/28/12
to mongoo...@googlegroups.com
If you have any other specific issues about mongoose, i'd be happy to try and answer them. if not, I recommend checking out the node.js mailing list or visiting the node.js IRC channel on freenode.


For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en

Aaron Heckmann

unread,
Jun 30, 2012, 1:40:25 PM6/30/12
to mongoo...@googlegroups.com
Best practice for connecting to mongo is to open the connection once and leave it open until you shut down you application. Don't close it after each save.

You can see when the connection opens by listening for its 'open' event:

    mongoose.connect(....)
    mongoose.connection.on('open', doStuff)

Yes, both socket.io connections can use a single mongo connection. You can also open more than one connection to mongo using mongoose.createConnection. There are also pooling options available for the connections.

For socket.io specific questions I'd ping their Google Group: http://groups.google.com/group/socket_io/
Reply all
Reply to author
Forward
0 new messages