Skip to content

Hodako/Python-Flask-Websocket-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python-Flask-Websocket-Server

You can run this on onrender.com and heroku

# Flask WebSocket Example

This is a simple Flask application that demonstrates how to implement a WebSocket API using Flask-SocketIO. The application allows clients to connect via WebSocket, send messages, and receive broadcast messages from the server.

## Features

- WebSocket support using Flask-SocketIO
- Broadcast messages to all connected clients
- Custom events handling

## Requirements

- Python 3.7+
- Flask
- Flask-SocketIO
- python-socketio
- eventlet

## Installation

1. Clone the repository:
    ```sh
    git clone https://github.com/yourusername/flask-websocket-example.git
    cd flask-websocket-example
    ```

2. Install the dependencies:
    ```sh
    pip install -r requirements.txt
    ```

## Running the Application

1. Start the Flask application:
    ```sh
    python app.py
    ```

2. Open your browser and navigate to `http://localhost:5000` to see the WebSocket example in action.

## Deployment

To deploy this application on Render, ensure you have the following additional files:

### `app.wsgi`

```python
from app import app

if __name__ == "__main__":
    app.run()

requirements.txt

Flask==2.1.1
Flask-SocketIO==5.3.2
python-socketio==5.5.2
eventlet==0.31.0

Usage

Once the application is running, open the index.html page in your browser. You can send messages and custom events using the input fields and buttons provided. The server will broadcast messages and respond to custom events, which will be displayed in the browser console.

Code Overview

app.py

from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit

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

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

@socketio.on('message')
def handle_message(msg):
    print('Message received: ' + msg)
    send('Message received: ' + msg, broadcast=True)

@socketio.on('custom_event')
def handle_custom_event(json):
    print('Custom event received: ' + str(json))
    emit('custom_response', {'response': 'Custom event received!'})

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

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Flask-SocketIO WebSocket Example</title>
    <script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', (event) => {
            var socket = io();

            socket.on('connect', function() {
                console.log('Connected to WebSocket server');
            });

            socket.on('message', function(msg) {
                console.log('Message from server: ' + msg);
            });

            socket.on('custom_response', function(data) {
                console.log('Custom response from server: ' + data.response);
            });

            function sendMessage() {
                var message = document.getElementById('messageInput').value;
                socket.send(message);
            }

            function sendCustomEvent() {
                var eventData = {data: 'Some custom data'};
                socket.emit('custom_event', eventData);
            }

            document.getElementById('sendBtn').addEventListener('click', sendMessage);
            document.getElementById('customEventBtn').addEventListener('click', sendCustomEvent);
        });
    </script>
</head>
<body>
    <h1>Flask-SocketIO WebSocket Example</h1>
    <input type="text" id="messageInput" placeholder="Enter a message">
    <button id="sendBtn">Send Message</button>
    <button id="customEventBtn">Send Custom Event</button>
</body>
</html>

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Acknowledgements


This README provides a comprehensive overview of your project, including installation instructions, usage examples, and code snippets for reference. Make sure to replace the GitHub URL with your actual repository link.

About

You can run this on onrender.com and heroku

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published