"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!