Skip to content

Commit f3427cb

Browse files
committed
Fixed duplicate messages being sent
1 parent 1d46a34 commit f3427cb

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

server/src/socket_server/methods/send_message.ts

+11
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@ export const sendMessage = (ctx: ConnectionContext, message: Message, ack: any):
77
if (message.location === undefined) {
88
message.location = ctx.user.location;
99
}
10+
if (message.location.lat > 90 || message.location.lat < -90 || message.location.lon > 180 || message.location.lon < -180) {
11+
console.log("[WS] User tried to send message with invalid location.");
12+
if (ack) ack("invalid_location");
13+
return;
14+
}
1015
message.timestamp = Date.now();
1116

1217
const recipients = getActiveUsersInView(message.location);
18+
const seen = new Set<string>(); // Geofire can return duplicates. Prevent sending message to the same user multiple times
1319
for (const recipient of recipients) {
1420
//if (recipient.uid == ctx.user.uid) continue; // skip sender
21+
22+
if (seen.has(recipient.uid)) continue;
23+
seen.add(recipient.uid);
24+
1525
recipient.socket.emit("message", message);
26+
console.log("[WS] Sent message to user <" + recipient.uid + ">");
1627
}
1728

1829
if (ack) ack("success");

0 commit comments

Comments
 (0)