Flask SocketIO not working in Azure App Service

Ziggy Zulueta 495 Reputation points MVP
2024-03-31T06:05:50.9566667+00:00

I have a simple chat app that works perfectly well in my localhost. It is a simple chat app that echoes whatever the user is inputting in the chat. When I deploy the deploy in Azure App Service the html page displays but nothing happens when the user inputs in the chat interface.

from flask import Flask, render_template
from flask_socketio import SocketIO
import os
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('chat.html')

@socketio.on('message')
def handle_message(data):
    print('received message: ' + data)
    socketio.send(data)

@socketio.on('connect')
def handle_connect():
    print('Client connected')

@socketio.on('disconnect')
def handle_disconnect():
    print('Client disconnected')

if __name__ == '__main__':
    socketio.run(app)


Here is the html page of chat.html

<!DOCTYPE html>
<html>
<head>
    <title>Chat Room</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <h1>Chat Room</h1>
    <ul id="messages"></ul>
    <form id="form" action="">
        <input id="input" autocomplete="off" autofocus/>
        <button>Send</button>
    </form>

    <script>
        var socket = io();
        socket.on('message', function(msg) {
            $('#messages').append($('<li>').text(msg));
        });
        $('#form').submit(function(e) {
            e.preventDefault();
            socket.send($('#input').val());
            $('#input').val('');
        });
    </script>
</body>
</html>
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

Accepted answer
  1. ajkuma 28,036 Reputation points Microsoft Employee Moderator
    2024-04-01T18:56:34.6466667+00:00

    Summarizing the answer shared by Ziggy Zulueta - Thanks for sharing the solution that worked for you with the community. It's much appreciated. Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others"

    Scenario:

    Provisioning simple chat app/Flask SocketIO to Azure App Service WebApp.

    Issue:

    Upon deploying the chat app to Azure App Service, the expected functionality, which echoes user inputs in the chat, is not working as intended. The app functions correctly on localhost. However, after deploying it to Azure App Service, while the HTML page displays, there is no response or functionality when users input messages in the chat interface.

    Resolution:

    Ziggy Zulueta was able to solve the problem by adding this line in the Startup command of the webapp: gunicorn -k eventlet -w 1 app:app as shown below:
    User's image

    In addition, SocketIO was modified accordingly in the code:

    socketio = SocketIO(app, async_mode='eventlet')
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ziggy Zulueta 495 Reputation points MVP
    2024-03-31T11:43:33.7566667+00:00

    I was able to solve the problem by adding this line in the Startup command of the webapp: gunicorn -k eventlet -w 1 app:app as shown below:

    User's image

    In addition, SocketIO must be modified accordingly in the code:

    socketio = SocketIO(app, async_mode='eventlet')
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.