Skip to content

Commit

Permalink
Adding websocket to flask with SocketIO
Browse files Browse the repository at this point in the history
  • Loading branch information
Rmarieta committed Jan 8, 2024
1 parent d4fefb3 commit 558febb
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
db:
image: postgres:15.2
image: postgres:16.1
container_name: db
env_file:
- .env
Expand Down
42 changes: 28 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
db:
image: postgres:15.2
image: postgres:16.1
container_name: db
env_file:
- .env
Expand All @@ -16,30 +16,44 @@ services:
timeout: 20s
retries: 5

redis:
image: redis:7.2.3
container_name: redis
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3

flask:
build: ./flask
container_name: flask
env_file:
- .env
ports:
- "5000:5000"
depends_on:
# waiting for the upstream containers to be ready
redis:
condition: service_healthy
db:
# waiting for the database to be ready
condition: service_healthy

volumes:
- ./flask:/app

nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/ssl-certs:/etc/nginx/ssl-certs
depends_on:
- flask
# nginx:
# image: nginx:latest
# container_name: nginx
# ports:
# - "80:80"
# - "443:443"
# volumes:
# - ./nginx/conf.d:/etc/nginx/conf.d
# - ./nginx/nginx.conf:/etc/nginx/nginx.conf
# - ./nginx/ssl-certs:/etc/nginx/ssl-certs
# depends_on:
# - flask

# to persist container volumes
volumes:
Expand Down
5 changes: 4 additions & 1 deletion flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ USER flaskuser
# CMD python application.py
# CMD python application.py run --debug --host 0.0.0.0

CMD gunicorn --access-logfile '-' --workers 2 --threads 3 --bind flask:5000 application:application
# without Flask-SocketIO
# CMD gunicorn --access-logfile '-' --workers 1 --threads 4 --bind flask:5000 application:application
# with Flask-SocketIO
CMD gunicorn --access-logfile '-' --worker-class eventlet --workers 1 --threads 4 --bind flask:5000 application:application
4 changes: 4 additions & 0 deletions flask/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from flask_socketio import SocketIO
import os

db = SQLAlchemy()
socketio = SocketIO()

def create_app():

Expand All @@ -20,8 +22,10 @@ def create_app():
port=os.environ['RDS_PORT'],
database=os.environ['RDS_DB_NAME'],
)
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']

db.init_app(app)
socketio.init_app(app)

# importing and registering routes with their url prefix
from .views.main import main_bp
Expand Down
1 change: 1 addition & 0 deletions flask/app/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sockets
15 changes: 15 additions & 0 deletions flask/app/views/sockets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask_socketio import emit, send
from app import socketio

@socketio.on('message')
def handle_message(message):
print('Received message:', message)
emit('response', {'data': 'Server received your message'})

@socketio.on('json')
def handle_json(json):
print('received json: ' + str(json))

@socketio.on('my event')
def handle_my_custom_event(json):
print('received my_event: ' + str(json))
9 changes: 9 additions & 0 deletions flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
bidict==0.22.1
click==8.1.7
dnspython==2.4.2
eventlet==0.34.2
Flask==2.2.3
Flask-Cors==3.0.10
Flask-SocketIO==5.3.6
Flask-SQLAlchemy==3.0.3
greenlet==2.0.2
gunicorn==21.2.0
h11==0.14.0
importlib-metadata==7.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
packaging==23.2
psycopg2-binary==2.9.6
python-engineio==4.8.2
python-socketio==5.11.0
simple-websocket==1.0.0
six==1.16.0
SQLAlchemy==2.0.9
typing_extensions==4.9.0
Werkzeug==2.2.3
wsproto==1.2.0
zipp==3.15.0
2 changes: 1 addition & 1 deletion nginx/conf.d/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ server {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Prefix /;
}
}
}

0 comments on commit 558febb

Please sign in to comment.