I am struggling with issue, my emit() function is not sending anything to frontend, despite that its receiving data. Here is my code:
Flask backend:
def ack():
print("message was received!")
@socketio.on("add_to_chat_event")
def handle_event(data):
print(data)
emit("my response", data, callback=ack, broadcast=True)
And here is my frontend:
function appendToChat(room, username) {
socket.emit("add_to_chat_event", { room: room, username: username });
}
$(".room").click(function () {
choiced_room = this.getAttribute("value");
socket = io();
socket.on("connect", function () {
joinRoom(choiced_room, username);
appendToChat(choiced_room, username);
});
socket.on("message", function (data) {
$("#messages").append(`<li class ='list-group'>${data}</li>`);
$("#Messages").value = "";
});
socket.on("add_to_chat_event", function (data) {
console.log(data, "tutaj");
if (!data) {
$(".users").append(`<li>Anonymous</li>`);
} else {
$(".users").append(`<li>${data}</li>`);
}
});
});
The problem is in the emit function becasue when I am printing received data its all fine. Am I missing something ? Thanks for the answer.
Problem is already solved, this is not mentioned in documentation. First parameter of emit() method is an listener for the frontend. In my situation i had to add to emit() method "add_to_chat_event" as first parameter instead of 'my response'