-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chores_lint_errors_and_path_issues * feature_db_py * fix_audio_issue * chores_lint_error * fix_openAPI_switching * chore_module_insert * fix_edit_collection_fix * fix_frontend_critical_update * fix_feedback2_translation_async * chore:lint * chore:loading_issue * chore:module * chore:version-python-socketio[asgi] * chore:main.py * chore:path
- Loading branch information
Serhii Ofii
authored
Oct 20, 2024
1 parent
c4f8491
commit 239902b
Showing
4 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ openai | |
requests | ||
stripe | ||
python-socketio | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Socket part.""" | ||
|
||
import socketio | ||
|
||
from linguaphoto.settings import settings | ||
|
||
# Create a new Socket.IO server with CORS enabled | ||
sio = socketio.AsyncServer( | ||
async_mode="asgi", | ||
cors_allowed_origins=[settings.homepage_url], # Update this to match your frontend URL | ||
) | ||
|
||
# Dictionary to store connected users by their socket ID | ||
connected_users: dict[str, str] = {} | ||
|
||
|
||
# Handle client connection | ||
@sio.event | ||
async def connect(sid: str) -> None: | ||
print(f"User connected: {sid}") | ||
|
||
|
||
# Handle client disconnection | ||
@sio.event | ||
async def disconnect(sid: str) -> None: | ||
if sid in connected_users: | ||
print(f"User {connected_users[sid]} disconnected") | ||
del connected_users[sid] | ||
|
||
|
||
# Event for registering a specific user (e.g., after authentication) | ||
@sio.event | ||
async def register_user(sid: str, user_id: str) -> None: | ||
connected_users[user_id] = sid | ||
print(f"User {user_id} registered with session ID {sid}") | ||
|
||
|
||
# Event to notify a specific user | ||
async def notify_user(user_id: str, message: dict) -> None: | ||
sid = connected_users.get(user_id) | ||
if sid: | ||
await sio.emit("notification", message, room=sid) | ||
else: | ||
print(f"User {user_id} is not connected") | ||
|
||
|
||
# Export the `sio` instance so it can be used in other files |