Skip to content

Commit 563a247

Browse files
authored
Merge pull request #98 from ruiqi7/feature/coding-sandbox
Add Redis instance for collab service
2 parents 6d5ef0a + af11aee commit 563a247

File tree

21 files changed

+182
-108
lines changed

21 files changed

+182
-108
lines changed

backend/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
3. Enter `host.docker.internal` as the Host.
1616

17+
4. Enter the port used by the respective service:
18+
- User Service: `6379`
19+
- Collab Service: `6380`
20+
1721
4. Follow the instructions [here](https://nodejs.org/en/download/package-manager) to set up Node v20.
1822

1923
## Setting-up cloud MongoDB (in production)

backend/code-execution-service/.env.sample

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
NODE_ENV=development
22
SERVICE_PORT=3004
33

4+
# Origins for cors
45
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
56

6-
# One Compiler
7+
# Other service APIs
8+
QUESTION_SERVICE_URL=http://question-service:3000/api/questions
9+
10+
# One Compiler configuration
711
ONE_COMPILER_URL=https://onecompiler-apis.p.rapidapi.com/api/v1/run
812
ONE_COMPILER_KEY=<ONE_COMPILER_KEY>
9-
10-
QUESTION_SERVICE_URL=http://question-service:3000/api/questions

backend/code-execution-service/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@
66

77
2. Sign up for a free OneCompiler API [here](https://rapidapi.com/onecompiler-onecompiler-default/api/onecompiler-apis).
88

9-
3. Update `ONE_COMPILER_KEY` in `.env` with the the value of `x-rapidapi-key`.
9+
3. Update `ONE_COMPILER_KEY` in the `.env` file with the value of `x-rapidapi-key`.
1010

11-
## Running Code Execution Service without Docker
11+
## Running Code Execution Service Locally
1212

1313
1. Open Command Line/Terminal and navigate into the `code-execution-service` directory.
1414

1515
2. Run the command: `npm install`. This will install all the necessary dependencies.
1616

1717
3. Run the command `npm start` to start the Code Execution Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
1818

19+
## Running Code Execution Service with Docker
20+
21+
1. Open the Command Line/Terminal.
22+
23+
2. Run the command `docker compose run code-execution-service` to start up the Code Execution Service and its dependencies.
24+
1925
## After running
2026

2127
1. To view Code Execution Service documentation, go to http://localhost:3004/docs.

backend/collab-service/.env.sample

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
NODE_ENV=development
22
SERVICE_PORT=3003
33

4+
# Origins for cors
45
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
56

6-
REDIS_URI=redis://redis:6379
7+
# Redis configuration
8+
REDIS_URI=redis://collab-service-redis:6379
79

8-
# Test
10+
# Tests
911
REDIS_URI_TEST=redis://test-redis:6379

backend/collab-service/README.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,49 @@
88

99
- `REDIS_URI`
1010

11-
## Running Collab Service Individually
11+
## Running Collab Service Locally
1212

13-
1. Set up and run Redis using `docker compose run --rm --name redis -p 6379:6379 redis`.
13+
1. Set up and run Redis using `docker compose run --rm --name collab-service-redis -p 6380:6379 collab-service-redis`.
1414

15-
2. Open Command Line/Terminal and navigate into the `collab-service` directory.
15+
2. Comment out `REDIS_URI` in the `.env` file.
1616

17-
3. Run the command: `npm install`. This will install all the necessary dependencies.
17+
3. Open Command Line/Terminal and navigate into the `collab-service` directory.
1818

19-
4. Run the command `npm start` to start the Collab Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
19+
4. Run the command `npm install`. This will install all the necessary dependencies.
2020

21-
## Running Collab Service Individually with Docker
21+
5. Run the command `npm start` to start the Collab Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
2222

23-
1. Open the command line/terminal.
23+
## Running Collab Service with Docker
2424

25-
2. Run the command `docker compose run collab-service` to start up the collab service and its dependencies.
25+
1. Open the Command Line/Terminal.
26+
27+
2. Run the command `docker compose run collab-service` to start up the Collab Service and its dependencies.
2628

2729
## After running
2830

2931
1. Using applications like Postman, you can interact with the Collab Service on port 3003. If you wish to change this, please update the `.env` file.
3032

3133
2. Setting up Socket.IO connection on Postman:
34+
3235
- You should open 2 tabs on Postman to simulate 2 users in the Collab Service.
3336

3437
- Select the `Socket.IO` option and set URL to `http://localhost:3003`. Click `Connect`.
35-
![image1.png](docs/image1.png)
36-
38+
![image1.png](docs/image1.png)
39+
3740
- Add the following events in the `Events` tab and listen to them.
38-
![image2.png](docs/image2.png)
39-
41+
![image2.png](docs/image2.png)
42+
4043
- To send a message, go to the `Message` tab and ensure that your message is being parsed as `JSON`.
41-
![image3.png](docs/image3.png)
42-
44+
![image3.png](docs/image3.png)
45+
4346
- In the `Event name` input, input the correct event name. Click on `Send` to send a message.
44-
![image4.png](docs/image4.png)
47+
![image4.png](docs/image4.png)
48+
49+
## Events Available
4550

46-
## Events Available
4751
| Event Name | Description | Parameters | Response Event |
48-
|----------------|-----------------------------------|-------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
52+
| -------------- | --------------------------------- | ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
4953
| **join** | Joins a collaboration room. | `roomId` (string): ID of the room. | **room_full:** Notify the user if the room is full (only 2 users allowed).<br/>**connected:** Notify the user if successfully connected.<br/>**new_user_connected:** Notify the other user if a new user joins the room. |
5054
| **change** | Sends updated code to other user. | `roomId` (string): ID of the room.<br/>`code` (string): Updated code content. | **code_change:** Notify the other user with the updated code content. |
5155
| **leave** | Leaves the collaboration room. | `roomId` (string): ID of the room. | **partner_left:** Notify the other user when one leaves the room. |
52-
| **disconnect** | Disconnects from the server. | None | **partner_disconnected:** Notify the other user when one is disconnected. |
56+
| **disconnect** | Disconnects from the server. | None | **partner_disconnected:** Notify the other user when one is disconnected. |

backend/collab-service/src/config/redis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dotenv.config();
66
const REDIS_URI =
77
process.env.NODE_ENV === "test"
88
? process.env.REDIS_URI_TEST
9-
: process.env.REDIS_URI || "redis://localhost:6379";
9+
: process.env.REDIS_URI || "redis://localhost:6380";
1010

1111
const client = createClient({ url: REDIS_URI });
1212

backend/collab-service/src/handlers/websocketHandler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ const getDocument = (roomId: string) => {
151151
let doc = collabSessions.get(roomId);
152152
if (!doc) {
153153
doc = new Doc();
154-
// eslint-disable-next-line
155-
doc.on(CollabEvents.UPDATE, (_update) => {
154+
doc.on(CollabEvents.UPDATE, () => {
156155
saveDocument(roomId, doc!);
157156
io.to(roomId).emit(CollabEvents.UPDATE, encodeStateAsUpdateV2(doc!));
158157
});
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
NODE_ENV=development
22
SERVER_PORT=3005
33

4-
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
4+
# Origins for cors
5+
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
NODE_ENV=development
22
SERVICE_PORT=3002
33

4+
# Origins for cors
45
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
56

6-
7-
## FOR RABBITMQ, comment out the variables under whichever use case (1) or (2) that is not applicable
8-
# (1) RabbitMq for running matching service individually
9-
RABBITMQ_ADDR=amqp://localhost:5672 #comment out if use case is (2)
10-
11-
# (2) RabbitMq for running matching service with other services using docker compose
12-
RABBITMQ_DEFAULT_USER=admin #comment out if use case is (1)
13-
RABBITMQ_DEFAULT_PASS=password #comment out if use case is (1)
14-
RABBITMQ_ADDR=amqp://admin:password@rabbitmq:5672 #comment out if use case is (1)
15-
7+
# Other service APIs
168
QUESTION_SERVICE_URL=http://question-service:3000/api/questions
17-
189
QN_HISTORY_SERVICE_URL=http://qn-history-service:3006/api/qnhistories
10+
11+
# RabbitMq configuration
12+
RABBITMQ_DEFAULT_USER=admin
13+
RABBITMQ_DEFAULT_PASS=password
14+
15+
## Do not change anything below this line
16+
RABBITMQ_ADDR=amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq:5672

backend/matching-service/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@
44
55
## Setting-up Matching Service
66

7-
1. In the `matching-service` directory, create a copy of the `.env.sample` file and name it `.env`. If you are looking to run matching service with the other services using docker-compose, comment out the variable `RABBITMQ_ADDR` under use case (1) in the .env file. Otherwise, if you are looking to run matching service individually, comment out the variables `RABBITMQ_DEFAULT_USER`, `RABBITMQ_DEFAULT_PASS` and `RABBITMQ_ADDR` under use case (2) in the .env file.
7+
1. In the `matching-service` directory, create a copy of the `.env.sample` file and name it `.env`.
88

9-
2. If you are running matching service together with other services using docker-compose, to set up credentials for RabbitMq, update the RabbitMq variables in the `.env` file. Update `RABBITMQ_DEFAULT_USER` and `RABBITMQ_DEFAULT_PASS` to what you want, then update `RABBITMQ_ADDR` to be `amqp://<RABBITMQ_DEFAULT_USER>:<RABBITMQ_DEFAULT_PASS>@rabbitmq:5672`. You can access RabbitMq management user interface locally with the username in `RABBITMQ_DEFAULT_USER` and password in `RABBITMQ_DEFAULT_PASS` at http://localhost:15672.
9+
2. You can access RabbitMq management user interface locally with `RABBITMQ_DEFAULT_USER` as the username and `RABBITMQ_DEFAULT_PASS` as the password at http://localhost:15672. You may update `RABBITMQ_DEFAULT_USER` and `RABBITMQ_DEFAULT_PASS` in the `.env` file to change your RabbitMq credentials if necessary.
1010

11-
3. If you are running matching service individually, you do not need to make any changes to `RABBITMQ_ADDR`. You can access RabbitMq management user interface locally with the username `guest` and password `guest` at http://localhost:15672.
11+
## Running Matching Service Locally
1212

13-
## Running Matching Service Individually with Docker
13+
1. Set up and run RabbitMq using `docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4.0-management`.
1414

15-
1. Set up and run RabbitMq locally on your computer with the command `docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4.0-management`.
15+
2. Comment out `RABBITMQ_ADDR` in the `.env` file. You can access RabbitMq management user interface locally with the username `guest` and password `guest` at http://localhost:15672.
1616

17-
2. Open Command Line/Terminal and navigate into the `matching-service` directory.
17+
3. Open Command Line/Terminal and navigate into the `matching-service` directory.
1818

19-
3. Run the command: `npm install`. This will install all the necessary dependencies.
19+
4. Run the command: `npm install`. This will install all the necessary dependencies.
2020

21-
4. Run the command `npm start` to start the Matching Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes. If you encounter connection errors, please wait for a few minutes before running `npm start` again as RabbitMq may take some time to start up.
21+
5. Run the command `npm start` to start the Matching Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes. If you encounter connection errors, please wait for a few minutes before running `npm start` again as RabbitMq may take some time to start up.
22+
23+
## Running Matching Service with Docker
24+
25+
1. Open the Command Line/Terminal.
26+
27+
2. Run the command `docker compose run matching-service` to start up the Matching Service and its dependencies.
2228

2329
## After running
2430

0 commit comments

Comments
 (0)