Skip to content

Commit

Permalink
feat: project dockerized and js script for connect to socket.io added…
Browse files Browse the repository at this point in the history
… to project.
  • Loading branch information
sinasezza committed Aug 11, 2024
1 parent b22b84e commit 6a0be94
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.12.4-slim-bullseye

WORKDIR /home

# Copy requirements first for better caching
COPY ./requirements.txt .


RUN python3 -m pip install -r requirements.txt && \
apt-get clean && \
apt-get autoclean && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/*

EXPOSE 8000


COPY ./entrypoint.sh .
COPY ./chatApp ./chatApp

# Set permissions for entrypoint
RUN chmod +x entrypoint.sh

CMD ["./entrypoint.sh"]
41 changes: 41 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
services:

fastapi-chat:
build: .
container_name: fastapi_chat
environment:
RUN_PORT: 8000
depends_on:
- mongodb-server
restart: unless-stopped
env_file:
- ./env/fast-api.env

mongodb-server:
image: mongodb/mongodb-community-server:latest
container_name: mongodb_server
restart: unless-stopped
env_file:
- ./env/mongodb.env
volumes:
- type: volume
source: mongodb_data_volume
target: /data/db
ports:
- '27117:27017'

nginx:
build: ./nginx
container_name: chat_nginx
restart: always
volumes:
# - ./nginx/configs/:/etc/nginx/conf.d
- ./uploads:/home/media
ports:
- 8000:80
depends_on:
- mongodb-server
- fastapi-chat

volumes:
mongodb_data_volume:
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

RUN_PORT=${RUN_PORT:-8000}

exec uvicorn chatApp.main:app --host 0.0.0.0 --port ${RUN_PORT}
18 changes: 18 additions & 0 deletions jsTester/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<script
src="https://cdn.socket.io/4.7.5/socket.io.min.js"
integrity="sha384-2huaZvOR9iDzHqslqwpR87isEmrfxqyWOF7hr7BY6KG0+hVKLoEXMPUJw3ynWuhO"
crossorigin="anonymous"
></script>
<script src="scripts.js"></script>
<title>Socket io tester</title>
</head>
<body>
<h1>welcome to Socket io tester</h1>
</body>
</html>
71 changes: 71 additions & 0 deletions jsTester/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const ROOM_ID = "66a7731a11fe8199049c8dbd"; // put your room ID here
const USER1_ID = "66a765df185ba34c8e4a4b1a"; // put your user ID here
const PRV_ROOM_ID = "66a785f6473ff39a7496f918"; // put your private room id to send message

// Establish the Socket.IO connection
const sio = io("http://127.0.0.1:8000/", {
path: "/socket.io",
transports: ['websocket', 'polling']
});

// Handle connection event
sio.on("connect", () => {
console.log("Connected to the server with SID:", sio.id);

// Emit the 'join_room' event for a public room
// sio.emit('joining_public_room', { room_id: ROOM_ID, user_id: USER1_ID });

// Send a public message
// sio.emit('send_public_message', { room_id: ROOM_ID, message: "Hello, world JS!", user_id: USER1_ID });

// emit the join_room event for a private room
// sio.emit('joining_private_room', {room_id: PRV_ROOM_ID, user_id: USER1_ID});

// Send a private message
// sio.emit('send_private_message', {room_id: PRV_ROOM_ID, user_id: USER1_ID, message: "hello private!"});
});

// Handle disconnection event
sio.on("disconnect", () => {
console.log("Disconnected from the server");
});

// Handle connection errors
sio.on("connect_error", (err) => {
console.log("Connection error:", err.message);
});

// Listen for the 'client_count' event
sio.on("client_count", (count) => {
console.log(`Number of connected clients: ${count}`);
});

// Listen for the 'room_count' event
sio.on("room_count", (count) => {
console.log(`Number of connected clients in the room: ${count}`);
});

// Listen for the 'user_joined' event
sio.on("user_joined", (user) => {
console.log(`${user} joined the chat room`);
});

// Listen for the 'user_left' event
sio.on("user_left", (user) => {
console.log(`${user} left the chat room`);
});

// Listen for the 'message' event
sio.on("message", ({ sid, message, message_id, user_id }) => {
console.log(`Message: ${message_id} with content ${message} from ${user_id}`);
});

// Listen for the 'private_message' event
sio.on("private_message", ({ sid, message, recipient_id, sender_id }) => {
console.log(`Private message: ${message} from ${sender_id} to ${recipient_id}`);
});

// Handle general errors
sio.on("error", (err) => {
console.error("An error occurred:", err);
});
Empty file added nginx/.Dockerignore
Empty file.
3 changes: 3 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM nginx

COPY configs/ /etc/nginx/conf.d/
35 changes: 35 additions & 0 deletions nginx/configs/chatApp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
upstream chat-app {
server fastapi-chat:8000;
}


server {
listen 80;
listen [::]:80;
server_name myChat.fastapi.org www.myChat.fastapi.org;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;


location /media/ {
autoindex on;
alias /home/media/;
}

location / {
proxy_pass http://chat-app;
proxy_ssl_server_name on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
}

location /socket.io/ {
proxy_pass http://chat-app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}

0 comments on commit 6a0be94

Please sign in to comment.