Skip to content

Commit e89f5e5

Browse files
committed
Update instructions on running matching-service with redis
1 parent ea0d3ed commit e89f5e5

File tree

2 files changed

+86
-58
lines changed

2 files changed

+86
-58
lines changed

apps/matching-service/README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ go mod tidy
2727
- `PORT`: Specifies the port for the WebSocket server. Default is `8081`.
2828
- `JWT_SECRET`: The secret key used to verify JWT tokens.
2929
- `MATCH_TIMEOUT`: The time in seconds to wait for a match before timing out.
30+
- `REDIS_URL`: The URL for the Redis server. Default is `localhost:6379`.
3031

31-
4. Start the WebSocket server:
32+
4. Start a local redis server:
33+
34+
```bash
35+
docker run -d -p 6379:6379 redis
36+
```
37+
38+
5. Start the WebSocket server:
3239

3340
```bash
3441
go run main.go
@@ -68,7 +75,9 @@ Client sends matching parameters:
6875
{
6976
"type": "match_request",
7077
"topics": ["Algorithms", "Arrays"],
71-
"difficulties": ["Easy", "Medium"]
78+
"difficulties": ["Easy", "Medium"],
79+
"username": "Jane Doe",
80+
"email": "[email protected]" // possible to change to user ID in mongodb
7281
}
7382
```
7483

@@ -77,9 +86,9 @@ Server response on successful match:
7786
```json
7887
{
7988
"type": "match_found",
80-
"matchID": 67890,
81-
"partnerID": 54321,
82-
"partnerName": "John Doe"
89+
"matchID": "aa41c004e589642402215c2c0a3a165a",
90+
"partnerID": 54321, // currently identifying via partner's websocket port --> change to user ID in mongodb
91+
"partnerName": "John Doe" // partner username
8392
}
8493
```
8594

@@ -100,6 +109,8 @@ Utilize `./tests/websocket-test.html` for a basic debugging interface of the mat
100109

101110
Make sure to open the HTML file in a web browser while the WebSocket server is running to perform your tests.
102111

112+
You can open one instance of the HTML file in multiple tabs to simulate multiple clients connecting to the server. (In the future: ensure that only one connection is allowed per user)
113+
103114
## Docker Support
104115

105116
TODO: Add section for Docker setup and usage instructions.
Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,83 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>Matching service: websocket test</title>
7-
</head>
8-
<body>
7+
</head>
8+
<body>
99
<p id="status">Status: no matching yet</p>
1010
<button onclick="openWebSocket()">Find match</button>
1111
<button onclick="closeConnection()">Cancel matching</button>
1212
<p id="response"></p>
1313
<script>
14-
let socket = null;
15-
16-
function openWebSocket() {
17-
// Connect to WebSocket server
18-
// User should be authenticated first
19-
// JWT token should be sent with the initial request, in the authorization header.
20-
// request would be checked as middleware.
21-
if (socket != null) return
22-
socket = new WebSocket("ws://localhost:8081/match");
23-
document.getElementById("response").innerText = "";
14+
let socket = null;
2415

25-
// Log connection opening
26-
socket.onopen = function() {
27-
console.log("WebSocket is open now.");
28-
29-
// send the match request when connection is open
30-
socket.send(JSON.stringify({
31-
type: "match_request",
32-
topics: ["Algorithms", "Arrays"],
33-
difficulties: ["Easy", "Medium"],
34-
}))
16+
function generateRandomDetails() {
17+
const username = Math.random().toString(36).substring(2, 8); // Generates a random username
18+
const domains = ["example.com", "mail.com", "test.com"];
19+
const domain = domains[Math.floor(Math.random() * domains.length)];
20+
return [username, `${username}@${domain}`];
21+
}
3522

36-
document.getElementById("status").innerText = "Status: matching in progress..."
37-
};
38-
39-
// Log response message and display it
40-
socket.onmessage = function(event) {
41-
console.log("Message received: " + event.data);
42-
document.getElementById("response").innerText = "Received: " + event.data;
43-
document.getElementById("status").innerText = "Status: matching found/timeout"
23+
function openWebSocket() {
24+
// Connect to WebSocket server
25+
// User should be authenticated first
26+
// JWT token should be sent with the initial request, in the authorization header.
27+
// request would be checked as middleware.
28+
if (socket != null) return;
29+
socket = new WebSocket("ws://localhost:8081/match");
30+
document.getElementById("response").innerText = "";
4431

45-
};
46-
47-
// Log errors
48-
socket.onerror = function(error) {
49-
console.log("WebSocket error: " + error);
50-
};
51-
52-
// Log connection closure
53-
socket.onclose = function() {
54-
console.log("WebSocket is closed now.");
55-
socket = null
56-
};
57-
}
58-
59-
function closeConnection() {
60-
document.getElementById("status").innerText = "Status: matching cancelled"
61-
document.getElementById("response").innerText = "";
62-
socket.close();
63-
}
32+
// Log connection opening
33+
socket.onopen = function () {
34+
console.log("WebSocket is open now.");
35+
36+
// Random email & username
37+
const arr = generateRandomDetails();
38+
39+
// send the match request when connection is open
40+
socket.send(
41+
JSON.stringify({
42+
type: "match_request",
43+
topics: ["Algorithms", "Arrays"],
44+
difficulties: ["Easy", "Medium"],
45+
username: arr[0],
46+
email: arr[1],
47+
})
48+
);
49+
50+
document.getElementById("status").innerText =
51+
"Status: matching in progress...";
52+
};
53+
54+
// Log response message and display it
55+
socket.onmessage = function (event) {
56+
console.log("Message received: " + event.data);
57+
document.getElementById("response").innerText =
58+
"Received: " + event.data;
59+
document.getElementById("status").innerText =
60+
"Status: matching found/timeout";
61+
};
62+
63+
// Log errors
64+
socket.onerror = function (error) {
65+
console.log("WebSocket error: " + error);
66+
};
67+
68+
// Log connection closure
69+
socket.onclose = function () {
70+
console.log("WebSocket is closed now.");
71+
socket = null;
72+
};
73+
}
74+
75+
function closeConnection() {
76+
document.getElementById("status").innerText =
77+
"Status: matching cancelled";
78+
document.getElementById("response").innerText = "";
79+
socket.close();
80+
}
6481
</script>
65-
</body>
82+
</body>
6683
</html>

0 commit comments

Comments
 (0)