Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:50:50+00:00 2026-06-17T12:50:50+00:00

I use socket.io to broadcast geolocation (lat, lng) and set new marker icon on

  • 0

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');
    });

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:50:51+00:00Added an answer on June 17, 2026 at 12:50 pm

    I think you’re generally on the right path. Here are the few easily noted issues:

    1. Where’s the push method come from? (You don’t have any arrays)
    2. Don’t you just want to set properties on a new instance of your MyModel when new data is received from the client?
    3. I’d think you’d create a new MyModel when the socket retrieves the send2DB data. Then, just set the properties.
    4. Also, you don’t want to make the 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in server -(multi)client application [TCP]. I use Socket, NetworkStream, StreamReader and StreamWriter for each
The first time I use Socket.Send(byte[] data) it does not throw an exception even
I'm trying to use the new room feature in Socket.io v.7 to create dynamic
I become use raw socket and i have this problem: bad file descriptor the
I am trying to use python socket instead of netcat to send metric data
I am getting start to socket use in C programming language. I am trying
Is It possible to create HTML5 Web Socket by use of ASP.NET/Web Form?
I use node.js and socket.io. I have an application that runs on the IP
I use SocketIO4Net for connecting to Socket.IO with C#, but when I try to
I am learning socket programming for use in an upcoming project, and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.