I use socket.io to broadcast geolocation (lat, lng) and set new marker icon on a map each time a user connects to the app. If a user in Japan connect, socket.io will send to me his position and he will receive my position too. Problem is that I don't want a user receive position of (and sent my position to) users that are far away, but only within a predefine radius (ex: 20 km).
Spatial search within mongo DB seems to be appropriate to identify users that are located in the same area and assign them to a groupId. Users of a same groupId would be allocated a common socket.io channel they will listen to.
To make my dream come true I'm actually trying to push user information into Mongo DB with mongoose. Problem is that mongoose run in the server while data are in the client.
I have define a schema.js to structure the database but I'm a little bit lost on how to setup the route to link client data with mongoose ? I understand that both mongoose and client must return the appropriate JSON object to communicate and data can be store with model.save()
. Sorry for this naive question but help on that point would be appreciate a lot. Thank you.
EDIT :
I finally use socket.io to pass user data to the server with send2DB
function but I'm facing this error :
Object { _id: 50fc08aebb97d3201d000001,
coords; [],
date: Sun Jan 20 2013 16:18:34 GMT+0100 (Paris, Madrid) } has no methode 'push'`
It seems to be a probelm with the array coords
but I don't understand why it returns the date too! Here is the code :
application.js :
// generate unique user id
var userId = Math.random().toString(16).substring(2,15);
// code here
onLocationfound = function(e){
userMarker.setLatLng(e.latlng);
map.setView(userMarker.getLatLng(),map.getZoom());
latitude = (e.latlng[0]||( Math.round(e.latlng.lat*1000)/1000));
longitude = (e.latlng[1]||( Math.round(e.latlng.lng*1000)/1000));
// send data to database (executed once)
sentData = {id: userId, coords: [{lat: latitude, lng: longitude}]}
socket.emit('send2DB', sentData);
server.js :
mongoose.connect('mongodb://localhost/test');
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
// create schema
var userInformation = new Schema({
username: { type: String, required: true, trim: true },
email: { type: String, required: true, trim: true },
role: { type: String, required: true, trim: true },
date: {type: Date, default: Date.now},
userId: String,
longitude: Number,
latitude: Number,
coords: [Number, Number]
});
// export retrieve my model
var MyModel = mongoose.model('ModelName', userInformation);
var instance = new MyModel();
// code here
io.sockets.on('connection', function (socket) {
// code here
// Store user data to DB then query spatial info //
socket.on('send2DB', function (data) {
console.log('Position is', data);
instance.push(
{userId: 'data.id'},
{latitude: 'data.lat'}
);
instance.save(function (err) {
if (!err) console.log('Success!')
else console.log('Error');
});
});
I think you're generally on the right path. Here are the few easily noted issues:
push
method come from? (You don't have any arrays) userId
the quoted string 'data.id'
, do you? Don't you just want the value, just as userId: data.id
? (As you probably want the values from the socket data
object) The basics can be found in the mongoose docs . .
socket.on('send2DB', function(data) {
var instance = new MyModel({userId: data.id, latitude: data.lat});
instance.save(function(err, instance) { /* callback */ });
});
Ultimately, you'd likely want to add some security and such to your API function and not just trust that the User.id is valid and the location.
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.