|
| 1 | +# Matching Service |
| 2 | + |
| 3 | +The Matching Service provides a WebSocket server to manage real-time matching requests through WebSocket connections. It is implemented in Go and uses the Gorilla WebSocket package to facilitate communication. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +1. Navigate to the matching service directory: |
| 8 | + |
| 9 | +```bash |
| 10 | +cd ./apps/matching-service |
| 11 | +``` |
| 12 | + |
| 13 | +2. Ensure that you have Go installed. Install the necessary dependencies: |
| 14 | + |
| 15 | +```bash |
| 16 | +go mod tidy |
| 17 | +``` |
| 18 | + |
| 19 | +3. Start the WebSocket server: |
| 20 | + |
| 21 | +```bash |
| 22 | +go run main.go |
| 23 | +``` |
| 24 | + |
| 25 | +## API Usage |
| 26 | + |
| 27 | +To establish a WebSocket connection with the matching service, use the following JavaScript code: |
| 28 | + |
| 29 | +```javascript |
| 30 | +const ws = new WebSocket("ws://localhost:8081/ws"); |
| 31 | +``` |
| 32 | + |
| 33 | +### Authentication |
| 34 | + |
| 35 | +The initial WebSocket request should include a JWT token that contains the user’s ID as a claim. This token will be verified by the server to authenticate the user. The user ID extracted is used to identify the client and facilitate the matching process. |
| 36 | + |
| 37 | +### Matching Workflow |
| 38 | + |
| 39 | +1. **Sending Matching Parameter**s: Once the WebSocket connection is established, the client sends a message containing the matching parameters (e.g., preferred topics or difficulty levels). |
| 40 | + |
| 41 | +2. **Receiving Match Results**: |
| 42 | + 2.1. **Match Found**: If a match is found, the server sends the matching result back to the client via the WebSocket connection. |
| 43 | + 2.2. **No Match Found**: If after a set amount of time, no match is found, the request timeouts, and the server sends a message that the matching failed. |
| 44 | + |
| 45 | +3. **Connection Closure**: |
| 46 | + 3.1. **Received Match Result**: After sending the match result, the server closes the connection. |
| 47 | + 3.2. **Cancellation**: If the user cancels the matching process, the client should close the connection. The server will recognize this and cancel the ongoing match. |
| 48 | + |
| 49 | +### Message Formats |
| 50 | + |
| 51 | +Provide the structure of the messages being sent back and forth between the server and the client. This includes the format of the initial matching parameters and the response payload. All requests should be in JSON and contain the `type` field to handle different types of messages. |
| 52 | + |
| 53 | +Client sends matching parameters: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "type": "match_request", |
| 58 | + "topics": ["Algorithms", "Arrays"], |
| 59 | + "difficulties": ["Easy", "Medium"] |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Server response on successful match: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "type": "match_found", |
| 68 | + "matchID": "67890", |
| 69 | + "partnerID": "54321", |
| 70 | + "partnerName": "John Doe" |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +If no match is found after a set period of time, the server will send a timeout message: |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "type": "timeout", |
| 79 | + "message": "No match found. Please try again later." |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +If the server encounters an issue during the WebSocket connection or processing, an error message will be sent back to the client before closing the connection. The client should handle these errors appropriately. |
| 84 | + |
| 85 | +Sample error structure: |
| 86 | + |
| 87 | +```json |
| 88 | +{ |
| 89 | + "type": "error", |
| 90 | + "message": "Invalid token" |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Environment Variables |
| 95 | + |
| 96 | +- `PORT`: Specifies the port for the WebSocket server. Default is `8081`. |
| 97 | +- `JWT_SECRET`: The secret key used to verify JWT tokens. |
| 98 | +- `MATCH_TIMEOUT`: The time in seconds to wait for a match before timing out. Default is `60` seconds. |
| 99 | + |
| 100 | +TODO: Add section for docker |
0 commit comments