-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from fahim-tazz/add-match-service
Add match service
- Loading branch information
Showing
7 changed files
with
1,400 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
// index.js | ||
const express = require('express'); | ||
const kafkaRoutes = require('./routes/matchingRoutes'); | ||
const { produceStartupMessage, runConsumer } = require('./controllers/matchingController'); | ||
const WebSocket = require("ws"); | ||
|
||
// Create an Express app | ||
const app = express(); | ||
const port = process.env.PORT || 3002; | ||
const wss = new WebSocket.Server({port: 3002}); | ||
const { matchmakeUser, runConsumer, dequeueUser} = require("./controllers/matchingController"); | ||
|
||
// Middleware to parse JSON bodies | ||
app.use(express.json()); | ||
console.log("Started Websocket server!!!"); | ||
|
||
// Use the Kafka routes | ||
app.use('/matching', kafkaRoutes); | ||
runConsumer().catch(console.error); | ||
|
||
// Start the Express server and Kafka consumer | ||
app.listen(port, async () => { | ||
console.log(`Matching service running on port ${port}`); | ||
wss.on("connection", (ws) => { | ||
console.log("New Client Connected"); | ||
ws.send("Welcome to websocket server"); | ||
|
||
// Produce a message on startup | ||
await produceStartupMessage(); | ||
ws.on('message', async (msg) => { | ||
msg = JSON.parse(msg) | ||
if (msg.event == "enqueue") { | ||
let res; | ||
try { | ||
res = await matchmakeUser(msg.userId, msg.questions) | ||
} catch (failure) { | ||
res = failure | ||
} | ||
ws.send(res) | ||
ws.close() | ||
} else if (msg.event == "dequeue") { | ||
dequeueUser(msg.userId); | ||
ws.close(); | ||
console.log("User has been dequeued") | ||
} | ||
}); | ||
|
||
// Start consuming messages | ||
runConsumer().catch(console.error); | ||
}); | ||
ws.on("close", () => { | ||
console.log("Client has disconnected") | ||
}); | ||
}) |
Oops, something went wrong.