Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend hotfixes #249

Merged
merged 5 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ build

# Private Key JSON
firebase-secrets.json
firebase-secret.json

# Other
.env
Expand Down
16 changes: 8 additions & 8 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const { createServer } = require("http");
const { Server } = require("socket.io");
const socket_port = process.env.socket_port;
const express_port = process.env.express_port;
const message_outreach_radius = Number(process.env.message_outreach_radius);
const app = express();

// Middleware
Expand Down Expand Up @@ -76,8 +77,8 @@ io.on("connection", async (socket: any) => {
await toggleUserConnectionStatus(socket.id);

const observer = messagesCollection
.order('lastUpdated', "desc")
.limit(0)
.orderBy('lastUpdated', "desc")
.limit(1)
.onSnapshot((querySnapshot) => {
querySnapshot.docChanges().forEach((change) => {
if (change.type === "added") {
Expand All @@ -96,20 +97,19 @@ io.on("connection", async (socket: any) => {
userLon
);

if (distance < 300) {
console.log("Message is within 300m of user");
if (distance < message_outreach_radius) {
console.log(`Message is within ${message_outreach_radius} meters of the user ${socket.id}.`);
socket.emit("message", change.doc.data());
} else {
console.log("Message is not within 300m of user");
console.log(`Message is not within ${message_outreach_radius} meters of the user ${socket.id}.`);
}
}
});
});
});

socket.on("disconnect", () => {
console.log(`[WS] User <${socket.id}> exited.`);
deleteConnectedUserByUID(socket.id);
observer();
});
socket.on("ping", (ack) => {
// The (ack) parameter stands for "acknowledgement." This function sends a message back to the originating socket.
Expand All @@ -122,7 +122,7 @@ io.on("connection", async (socket: any) => {
try {
const messageCreated = await createMessage(message);
if (!messageCreated) throw new Error("createMessage() failed.");
if (ack) ack("message recieved");
if (ack) ack("message recieved");
} catch (error) {
console.error("[WS] Error sending message:", error.message);
}
Expand Down
Loading