1

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

1 Answer 1

1

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):
    # ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.