Skip to content

Commit

Permalink
Small modifs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rmarieta committed Jan 12, 2024
1 parent 8a1dcf4 commit e53c690
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The backend consists in:

- A PostgreSQL database.
- An Nginx load balancer.
- Flask containers running behind the load balancer and served over Gunicorn.
- Flask containers running behind the load balancer and served over Gunicorn to enable multi-threading.
- A Redis container to coordinate websocket connections/messages while having multiple Flask processes running at the same time.

To start the development containers :
Expand Down
2 changes: 1 addition & 1 deletion flask/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_app():
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']

db.init_app(app)
socketio.init_app(app, message_queue='redis://redis')
socketio.init_app(app, message_queue=os.environ.get('REDIS_MESSAGE_QUEUE_URL'))

# importing and registering routes with their url prefix
from .views.main import main_bp
Expand Down
24 changes: 22 additions & 2 deletions flask/app/views/sockets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask_socketio import emit, send
from flask_socketio import emit, send, join_room, leave_room
from app import socketio
import os

@socketio.on('message')
def handle_message(message):
Expand All @@ -12,4 +13,23 @@ def handle_json(json):

@socketio.on('my event')
def handle_my_custom_event(json):
print('received my_event: ' + str(json))
print('received my_event: ' + str(json))

@socketio.on('chat')
def chat(data):
print("chat "+str(data))
emit('chat', "From hostname " + os.uname().nodename + " : " + data['message'], broadcast=True, to=data['room'])

@socketio.on('join')
def on_join(data):
username = data['username']
room = data['room']
join_room(room)
send("From hostname " + os.uname().nodename + " : " + username + ' has entered the room.', to=room)

@socketio.on('leave')
def on_leave(data):
username = data['username']
room = data['room']
leave_room(room)
send("From hostname " + os.uname().nodename + " : " + username + ' has left the room.', to=room)

0 comments on commit e53c690

Please sign in to comment.