Reputation: 23
i try to make simple chat app with flask as project so i have page
when user make new chat room the chat room appear to all of other users
so i have this code but socket.emit doesn't work on it
i mean the flask app doesn't receive the data here is the code
document.addEventListener('DOMContentLoaded',function () {
// Connect to websocket
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port+'/channels');
// When connected, configure buttons
socket.on('connect', function () {
document.querySelector("#create").onclick=function (){
const new_channel=document.querySelector("#new_chanell").value;
const channels_list=document.querySelector("#channels");
for (let i=0; i<channels_list.length;i++){
if(channels_list[i].value==new_chanell){
document.querySelector("h1").innerHTML="this channel allready here";
return false;}
}
socket.emit('add_channel', {'channel': new_channel});
document.querySelector("h1").innerHTML="new channel";
return false;
}});
and in flask
@socketio.on("add_channel")
def add_channel(data):
raise("he")
channel=data["channel"]
channel_list.append(channel)
emit("new_one",{"channel":channel},broadcast=True)
so i put raise("he") to know if the app receive the data but no
the function doesn't call at all why
Upvotes: 1
Views: 484
Reputation: 67502
On the connection URL you are passing a /channels
namespace. If that is intended, then on the server you have to add the namespace into all your event handlers:
@socketio.on("add_channel", namespace='/channels')
def add_channel(data):
# ...
Upvotes: 1