-
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.
- Loading branch information
Showing
13 changed files
with
69 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,15 @@ | ||
import json | ||
|
||
from channels.db import database_sync_to_async | ||
from channels.exceptions import StopConsumer | ||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
from channels.generic.websocket import AsyncJsonWebsocketConsumer | ||
|
||
from app import models | ||
|
||
class NotificationsConsumer(AsyncJsonWebsocketConsumer): | ||
groups = ["notification"] | ||
|
||
class MyAsyncWebSocketConsumer(AsyncWebsocketConsumer): | ||
async def connect(self): | ||
print("Web socket connected") | ||
print("Channel layer ", self.channel_layer) | ||
print("Channel name", self.channel_name) | ||
await self.accept() | ||
user = await database_sync_to_async(models.User.objects.get)( | ||
email=self.scope["user"].username | ||
) | ||
self.group_name = user.first_name | ||
print(self.group_name) | ||
await self.channel_layer.group_add(self.group_name, self.channel_name) | ||
|
||
async def receive(self, text_data=None, bytes_data=None): | ||
print("Message recieved from client...", text_data) | ||
|
||
async def notification_send(self, event): | ||
print(event) | ||
await self.send(text_data=json.dumps({"msg": event["message"]})) | ||
|
||
async def disconnect(self, close_code): | ||
print("Web socket disconnect") | ||
await self.channel_layer.group_discard(self.group_name, self.channel_name) | ||
raise StopConsumer() | ||
async def push(self, event): | ||
await self.send_json(content={"notifications": event["message"]}) |
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,14 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from app.utils import push_notification | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Send Test Alert to Websocket" | ||
|
||
def handle(self, *args, **options): | ||
msg = "Sending test notifications" | ||
|
||
push_notification(msg) | ||
|
||
self.stdout.write(self.style.SUCCESS("Done")) |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from django.urls import path | ||
from django.urls import re_path | ||
|
||
from . import consumers | ||
|
||
websocket_urlpatterns = [ | ||
path("ws/awc/", consumers.MyAsyncWebSocketConsumer.as_asgi()), | ||
re_path(r"ws/notifications/$", consumers.NotificationsConsumer.as_asgi()), | ||
] |
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
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
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
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