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 7815083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:24:06+00:00 2026-06-02T05:24:06+00:00

Using socket.io, I would like to broadcast a message sent by the user only

  • 0

“Using socket.io, I would like to broadcast a message sent by the user only to a subset of all clients. The subset would lie within a certain distance of the user emitting the message (say 1km). It seems I would need to add something like this (http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points) to the [socket.on ‘broadcast’, (message)] section of the code below (please note this is coffeescript and pulled from Shoaib Burq’s geochat-example github repo (https://github.com/sabman/geochat-example)).”

Now including Ricardo’s suggestions, replacing the old [socket.on ‘broadcast’, (message) ->] section with his input (although I changed “user” to “record” to stay consistent with the rest of the server.coffee code), and the [find:] section at the end. Something is wrong, though, and I’m not sure if it has to do with the html code or the updates to the coffeescript. Thanks again in advance..

Code still isn’t running. Including Ricardo’s newest suggestions:

 socket.on 'broadcast', (message) ->
    # find the sender and extract it's position
    Character.find { clientId: socket.id }, (record) =>
        chat_data =
            user: record
            conversation: message
        [lat, lng] = record.geometry.coordinates
        # find everyone within a 1km square
        km = 1/111
         area = 
            type: 'Polygon'
            coordinates: [
               [lat - km, lng - km]
               [lat + km, lng - km]
               [lat + km, lng + km]
               [lat - km, lng + km]
            ]
        Character.find { within: area }, (record) ->
            // send message to each user

     socket.broadcast.send JSON.stringify(chat_data)

I have a feeling the problem may lie with the ‘find’ section. For instance, Ricardo wrote

    console.log ">> find #{JSON.stringify(attrs)}"

But in the original code, this reads with a “=>” instead of “>>”:

    console.log "=> find #{JSON.stringify(attrs)}"

qs = require 'querystring'

find: (query, callback) ->
    console.log ">> find #{JSON.stringify(attrs)}"

    q = { operator: "or",  properties: attrs }
    url = lyr_config_characters.api_url
    key = lyr_config_characters.acl.get

    if query.id?
      url += "/#{attrs.id}?" + qs.stringify({ key })
    else if query.within?
      url += "/functions/within?" + qs.stringify({ key, input : query.within })
    else
      url += qs.stringify { key, input: q }

    req =
        method: "GET"
        uri: url
        headers: { "Content-Type": "application/json" }

    request req, (error, response, body) ->
        console.log error if error
        console.log "<< find #{body}"
        callback JSON.parse body ? '{}'

Thank you!

  • 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-02T05:24:08+00:00Added an answer on June 2, 2026 at 5:24 am

    The example you are using is based on spatialdb, which seems to have very incomplete documentation.

    From the looks of it, you need to query users using a geometry object, it will return points (users) that are contained within:

    socket.on 'broadcast', (message) ->
        # find the sender and extract it's position
        Character.find { clientId: socket.id }, (user) ->
            chat_data =
                user: user
                conversation: message
            [lat, lng] = user.geometry.coordinates
            # find everyone within a 1km square
            km = 1/111
            area = 
                type: 'Polygon'
                coordinates: [
                   [lat - km, lng - km]
                   [lat + km, lng - km]
                   [lat + km, lng + km]
                   [lat - km, lng + km]
                ]
            Character.find { within: area }, (users) ->
                // send message to each user
    

    You’ll also need to modify the find method to use the within query:

    find: (attrs, callback) ->
        console.log "=> find #{JSON.stringify(attrs)}"
        q = { operator: "or",  properties: attrs }
        if _.contains(_.keys(attrs), "id")
          url = "#{lyr_config_characters.api_url}/#{attrs.id}?key=#{lyr_config_characters.acl.get}"
        else if _.contains(_.keys(attrs), "within")
          url = "#{lyr_config_characters.api_url}/functions/within?key=#{lyr_config_characters.acl.get}&input=#{encodeURIComponent(JSON.stringify(attrs.within))}"
        else
          url = "#{lyr_config_characters.api_url}?key=#{lyr_config_characters.acl.get}&input=#{encodeURIComponent(JSON.stringify(q))}"
        req = { method: "GET", uri: url, headers: {"Content-Type": "application/json"} }
    

    (which, for sanity, I would rewrite)

    qs = require 'querystring'
    
    find: (query, callback) ->
        console.log ">> find #{JSON.stringify(attrs)}"
    
        q = { operator: "or",  properties: attrs }
        url = lyr_config_characters.api_url
        key = lyr_config_characters.acl.get
    
        if query.id?
          url += "/#{attrs.id}?" + qs.stringify({ key })
        else if query.within?
          url += "/functions/within?" + qs.stringify({ key, input : query.within })
        else
          url += qs.stringify { key, input: q }
    
        req =
            method: "GET"
            uri: url
            headers: { "Content-Type": "application/json" }
    
        request req, (error, response, body) ->
            console.log error if error
            console.log "<< find #{body}"
            callback JSON.parse body ? '{}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know: when programming in C using a socket (AF_UNIX) is
I am using Boost Asio to set up a socket connection. I would like
I'm using the socket.makefile method to create a file-like object on a UDP socket
I have been using Sinatra for sometime now and I would like to add
I would like to write a program to receive some data using tcpClient from
There's already a working server service via socket, and I would like to test
I have build a game using canvas and Javascript, and I would like to
I'm using Intent(context, destinationClass) to switch activities. However, I would like to bring some
1) How would I connect through a proxy if I am using the Socket
I would like to understand how socket work, especially I need some code samples

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.