To send messages in real time, I have configured Django, channels, Redis, and Daphne. When it is being developed, it functions exactly as planned, but as soon as it is deployed to the live server, it stops functioning. The problem is that, contrary to my expectations, when I type a message in the input field and click the submit button, the page reloads. I’m not sure if the messages are actually transmitted or not when the page reloads, but because my database does not save the messages, I assume they are not.
I have preventDefault() in the javascript, or is there something else to do while on a live server?
This is the script that makes the call to the channels
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function () {
var receiver = null;
var receiver_id = "{{ reciever.username }}";
var logged_in = "{{ request.user.username }}";
const pathname = window.location.pathname;
const parts = pathname.split("/");
const username = parts[parts.length - 2];
if (receiver_id === logged_in) {
receiver = receiver_id;
} else {
receiver = receiver_id;
}
var socket = new WebSocket("ws://" + window.location.host + "/ws/chat/" + receiver + "/");
socket.onopen = function () {
console.log("WebSocket connection established.");
};
var typingTimer;
var typingTimeout = 1000;
$("#chat-input").on("input", function () {
clearTimeout(typingTimer);
typingTimer = setTimeout(function () {
$("#typing-status").text("");
}, typingTimeout);
$("#typing-status").text("typing...");
});
socket.onmessage = function (event) {
var data = JSON.parse(event.data);
var message = data.message;
var sender = data.sender;
var profileImage = data.profile_image;
var reciever = data.reciever;
var typing = data.typing;
if (sender === "{{sender}}") {
var chatMessage = '<div class="d-flex flex-row justify-content-end message">';
chatMessage += '<div style="margin-left: 20px;">';
chatMessage += '<p class="small p-2 me-3 mb-1 text-white rounded-3 custom-bg">' + message + "</p>";
chatMessage += '<p class="small me-3 mb-3 rounded-3 text-muted">0 minute ago</p>';
chatMessage += "</div>";
chatMessage += '<img style="width: 50px; height: 50px; object-fit: cover; border-radius: 50%; " src="' + profileImage + '" class="">';
chatMessage += "</div>";
$("#chat-messages").append(chatMessage);
var chatContainer = document.querySelector(".chat_container");
chatContainer.scrollTop = chatContainer.scrollHeight;
console.log("Yes");
} else {
var chatMessage = '<div class="d-flex flex-row justify-content-start message">';
chatMessage += '<img style="width: 50px; height: 50px; object-fit: cover; border-radius: 50%;" src="' + profileImage + '" class="">';
chatMessage += '<div style=" padding-left: 13px;" >';
chatMessage += '<p class="small p-2 me-3 mb-1 rounded-3" style="background-color: #f5f6f7;">' + message + "</p>";
chatMessage += '<p class="small me-3 mb-3 rounded-3 text-muted">0 minute ago</p>';
chatMessage += "</div>";
chatMessage += "</div>";
$("#chat-messages").append(chatMessage);
var chatContainer = document.querySelector(".chat_container");
chatContainer.scrollTop = chatContainer.scrollHeight;
console.log("No");
}
if (typing && sender !== logged_in) {
$("#typing-status").text("typing...");
} else {
$("#typing-status").text("");
}
};
socket.onclose = function () {
console.log("WebSocket connection closed.");
};
$("#chat-send").on("submit", function (e) {
e.preventDefault();
var input = $("#chat-input");
var message = input.val();
var sender = "{{request.user.username}}";
var data = {
message: message,
sender: sender,
reciever: username,
typing: false,
};
socket.send(JSON.stringify(data));
input.val("");
var $proceedButton = $("#proceed-btn");
$proceedButton.prop("disabled", true);
$(".chat_container").scrollTop(100000000000);
$("#message-form")[0].reset();
});
});
$(document).ready(function () {
$(".chat_container").scrollTop(100000000000);
$("#message-form").preventDefault();
});
</script>