Currently, the only prints that are not outputting are the ones in the frNotifier, heres settings.py:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
}
ASGI_APPLICATION = 'app_dependencies.asgi.application'
consumers.py:
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
from api.models import friendRequest
from api.models import User
import json
class FRConsumer(WebsocketConsumer):
def connect(self):
if self.scope["user"].is_anonymous:
# Reject the connection
self.close()
else:
# Accept the connection
user = self.scope['user']
self.group_name = f'noti_{user.userID}'
async_to_sync(self.channel_layer.group_add(self.group_name, self.channel_name))
self.accept()
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard(self.group_name, self.channel_name))
def receive(self, text_data):
print(f"{text_data}")
jData = json.loads(text_data)
reqID = jData["requestID"]
receiverUserID = friendRequest.objects.filter(requestID=reqID).values("receiver")[0]["receiver"]
senderUserID = friendRequest.objects.filter(requestID=reqID).values("sender")[0]["sender"]
sender = User.objects.filter(userID=senderUserID).values("username")[0]["username"]
targetGrp = f'noti_{receiverUserID}'
receiver = User.objects.filter(userID=receiverUserID).values("username")[0]["username"]
# Truth table variables
print(f"""------------- TRUTH TABLE -------------
Target Group: {targetGrp}
Receiver: {receiver}
Sender: {sender}
Own Group Name: {self.group_name}
---------------- OUTPUTS ---------------""")
try:
print("Sending to..." + targetGrp)
async_to_sync(
self.channel_layer.group_send)(
targetGrp,
{
'type': 'frNotifier',
'requestID': reqID,
'sender': sender
}
)
print("Succesfully sent message to group")
except Exception:
print("User not online")
def frNotifier(self, textData):
print("FRNotifier called")
try:
reqID = textData['requestID']
sender = textData['sender']
async_to_sync(self.send(textData=json.dumps({
'requestID':reqID,
'senderUsername': sender
})))
print("Friend request notification sent")
except Exception as e:
print("ERROR: User is not online for FR notification.")
My issue is that frNotifier is not being called on the receiving consumer’s/group’s end.
How are you running your project?
This is a really bad idea once you get beyond the stage of prototyping.
To my knowledge I am only to use redis when I deploy the application not during prototyping. Not sure what the proper way is, I’m still quite a beginner - my bad. But I am running my project on localhost.
I only ever use Redis - even in development. I don’t know of any benefit or advantage to using that “InMemoryChannelLayer”.
Was following the docs, I’ll switch to redis then and see if theres a difference.
I’m not saying that this is the cause of this issue - only that it’s something that should be addressed sooner rather than later.
What are you seeing in your logs from all the print statements? It might be helpful to see the complete console log from your testing.
Well the WebSocket handshakes are completed and the outputs look like this:
{"requestID":"5361f63d-c921-4043-9990-853dfc65e77b"}
------------- TRUTH TABLE -------------
Target Group: noti_16bb0918-aa22-44e3-8864-8b639a915190
Receiver: Frank
Sender: Jerry Kole
Own Group Name: noti_3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e
---------------- OUTPUTS ---------------
Sending to...noti_16bb0918-aa22-44e3-8864-8b639a915190
Succesfully sent message to group
It just blacks out after the receive outputs and no output is given for the frNotifier.
What output do you have showing that this is a valid group name with a connected user?
Not sure how to check that with InMemoryChannelLayer. Would I have to create a model to keep track of this?
You could print the users as they connect - and as long as there’s no disconnect message, that user should remain connected.
Oh I did that already, the client’s channel is assigned to the correct group.
But you haven’t shown that in the output you posted.
Please post the complete log, showing all the information from your test.
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:53960]
---- Jerry Kole with a userID of 3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e is in the group noti_3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:53960]
...
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:44112]
---- Frank with a userID of 16bb0918-aa22-44e3-8864-8b639a915190 is in the group noti_16bb0918-aa22-44e3-8864-8b639a915190.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:44112]
function:
def connect(self):
if self.scope["user"].is_anonymous:
# Reject the connection
self.close()
else:
# Accept the connection
user = self.scope['user']
self.group_name = f'noti_{user.userID}'
async_to_sync(self.channel_layer.group_add(self.group_name, self.channel_name))
print(f"---- {user.username} with a userID of {user.userID} is in the group {self.group_name}.")
self.accept()
Please post the complete log from the entire test.
Just GET requests:
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:53960]
---- Jerry Kole with a userID of 3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e is in the group noti_3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:53960]
WebSocket DISCONNECT /ws/notifyFR/ [127.0.0.1:48290]
HTTP GET /dashboard/ 200 [0.01, 127.0.0.1:44088]
HTTP GET /static/dashboard/main/main.css 200 [0.00, 127.0.0.1:44088]
HTTP GET /static/dashboard/main/main.js 200 [0.00, 127.0.0.1:44100]
HTTP GET /static/dummyUser.png 200 [0.00, 127.0.0.1:44100]
HTTP GET /uploads/profiles/user_16bb0918-aa22-44e3-8864-8b639a915190.jpeg 200 [0.01, 127.0.0.1:44088]
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:44112]
---- Frank with a userID of 16bb0918-aa22-44e3-8864-8b639a915190 is in the group noti_16bb0918-aa22-44e3-8864-8b639a915190.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:44112]
The initial disconnect is the app resetting.
This output doesn’t include the other information shown earlier.
Please post the complete log from the entire test.
I don’t understand, the outputs are the same with a difference of the requests in-between. I am only showing the WebSockets establishing connections. Do you want the web server starting up included aswell?
Please post the complete log from the entire test.
Alright, completely unfiltered:
***apps.py loaded***
***signals.py loaded***
Watching for file changes with StatReloader
Performing system checks...
***forms.py loaded***
System check identified no issues (0 silenced).
January 29, 2025 - 23:43:11
Django version 5.1.4, using settings 'app_dependencies.settings'
Starting ASGI/Daphne version 4.1.2 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
HTTP GET /dashboard/ 200 [0.01, 127.0.0.1:48264]
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:48266]
---- Jerry Kole with a userID of 3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e is in the group noti_3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:48266]
HTTP GET /dashboard/ 200 [0.01, 127.0.0.1:48274]
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:48290]
---- Frank with a userID of 16bb0918-aa22-44e3-8864-8b639a915190 is in the group noti_16bb0918-aa22-44e3-8864-8b639a915190.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:48290]
WebSocket DISCONNECT /ws/notifyFR/ [127.0.0.1:48266]
HTTP GET /dashboard/ 200 [0.03, 127.0.0.1:48264]
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:53940]
---- Jerry Kole with a userID of 3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e is in the group noti_3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:53940]
WebSocket DISCONNECT /ws/notifyFR/ [127.0.0.1:53940]
HTTP GET /dashboard/ 200 [0.01, 127.0.0.1:53942]
HTTP GET /static/dashboard/main/main.css 200 [0.00, 127.0.0.1:53942]
HTTP GET /static/dashboard/main/main.js 200 [0.00, 127.0.0.1:53952]
HTTP GET /static/dummyUser.png 200 [0.00, 127.0.0.1:53942]
HTTP GET /uploads/profiles/user_3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e.jpeg 200 [0.01, 127.0.0.1:53952]
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:53960]
---- Jerry Kole with a userID of 3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e is in the group noti_3f1e0afe-64ac-4ce4-b5e3-dd5cb4b4555e.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:53960]
WebSocket DISCONNECT /ws/notifyFR/ [127.0.0.1:48290]
HTTP GET /dashboard/ 200 [0.01, 127.0.0.1:44088]
HTTP GET /static/dashboard/main/main.css 200 [0.00, 127.0.0.1:44088]
HTTP GET /static/dashboard/main/main.js 200 [0.00, 127.0.0.1:44100]
HTTP GET /static/dummyUser.png 200 [0.00, 127.0.0.1:44100]
HTTP GET /uploads/profiles/user_16bb0918-aa22-44e3-8864-8b639a915190.jpeg 200 [0.01, 127.0.0.1:44088]
WebSocket HANDSHAKING /ws/notifyFR/ [127.0.0.1:44112]
---- Frank with a userID of 16bb0918-aa22-44e3-8864-8b639a915190 is in the group noti_16bb0918-aa22-44e3-8864-8b639a915190.
WebSocket CONNECT /ws/notifyFR/ [127.0.0.1:44112]