├── .gitignore ├── LICENSE ├── README.md ├── audio ├── __init__.py ├── audio.py ├── static │ ├── _files │ │ └── README.md │ └── audio │ │ ├── main.js │ │ ├── mic128.png │ │ └── styles.css └── templates │ └── audio │ └── main.html ├── chat ├── __init__.py ├── chat.py ├── static │ └── chat │ │ ├── main.js │ │ └── styles.css └── templates │ └── chat │ └── main.html ├── polls ├── __init__.py ├── polls.py ├── static │ └── polls │ │ ├── main.js │ │ ├── styles.css │ │ └── vote.js └── templates │ └── polls │ ├── main.html │ └── vote.html ├── requirements.txt ├── socketio_examples.py ├── static └── _files │ └── README.md ├── templates └── index.html ├── uploads ├── __init__.py ├── static │ ├── _files │ │ └── README.md │ └── uploads │ │ ├── main.js │ │ └── styles.css ├── templates │ └── uploads │ │ └── main.html └── uploads.py └── where ├── __init__.py ├── static └── where │ ├── drop_pin.js │ ├── main.js │ └── styles.css ├── templates └── where │ ├── drop_pin.html │ └── main.html └── where.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Miguel Grinberg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Socket.IO Examples 2 | 3 | This repository contains a few examples that demonstrate the features of the 4 | Python Socket.IO server in combination with Flask. 5 | 6 | ## How to Run 7 | 8 | First create a virtual environment and import the requirements. 9 | 10 | One of the demos uses the Google Maps API. For that demo to work you need to 11 | request a Google Maps API key from Google, as described 12 | [here](https://developers.google.com/maps/documentation/javascript/get-api-key). 13 | 14 | To start the server, run: 15 | 16 | ``` 17 | (venv) $ export GOOGLE_MAPS_KEY= 18 | (venv) $ export FLASK_APP=socketio_examples.py 19 | (venv) $ flask run 20 | ``` 21 | 22 | Finally, open _http://localhost:5000_ on your web browser to access the 23 | application. 24 | 25 | Note: You can run the application without a Google Maps key. All the demos 26 | except "Where do you live?" will work just fine. 27 | -------------------------------------------------------------------------------- /audio/__init__.py: -------------------------------------------------------------------------------- 1 | from .audio import bp 2 | -------------------------------------------------------------------------------- /audio/audio.py: -------------------------------------------------------------------------------- 1 | """Audio Recording Socket.IO Example 2 | 3 | Implements server-side audio recording. 4 | """ 5 | import os 6 | import uuid 7 | import wave 8 | from flask import Blueprint, current_app, session, url_for, render_template 9 | from flask_socketio import emit 10 | from socketio_examples import socketio 11 | 12 | bp = Blueprint('audio', __name__, static_folder='static', 13 | template_folder='templates') 14 | 15 | 16 | @bp.route('/') 17 | def index(): 18 | """Return the client application.""" 19 | return render_template('audio/main.html') 20 | 21 | 22 | @socketio.on('start-recording', namespace='/audio') 23 | def start_recording(options): 24 | """Start recording audio from the client.""" 25 | id = uuid.uuid4().hex # server-side filename 26 | session['wavename'] = id + '.wav' 27 | wf = wave.open(current_app.config['FILEDIR'] + session['wavename'], 'wb') 28 | wf.setnchannels(options.get('numChannels', 1)) 29 | wf.setsampwidth(options.get('bps', 16) // 8) 30 | wf.setframerate(options.get('fps', 44100)) 31 | session['wavefile'] = wf 32 | 33 | 34 | @socketio.on('write-audio', namespace='/audio') 35 | def write_audio(data): 36 | """Write a chunk of audio from the client.""" 37 | session['wavefile'].writeframes(data) 38 | 39 | 40 | @socketio.on('end-recording', namespace='/audio') 41 | def end_recording(): 42 | """Stop recording audio from the client.""" 43 | emit('add-wavefile', url_for('static', 44 | filename='_files/' + session['wavename'])) 45 | session['wavefile'].close() 46 | del session['wavefile'] 47 | del session['wavename'] 48 | -------------------------------------------------------------------------------- /audio/static/_files/README.md: -------------------------------------------------------------------------------- 1 | File uploads are stored in this directory. 2 | -------------------------------------------------------------------------------- /audio/static/audio/main.js: -------------------------------------------------------------------------------- 1 | /* Audio recording and streaming demo by Miguel Grinberg. 2 | 3 | Adapted from https://webaudiodemos.appspot.com/AudioRecorder 4 | Copyright 2013 Chris Wilson 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | 19 | window.AudioContext = window.AudioContext || window.webkitAudioContext; 20 | 21 | var audioContext = new AudioContext(); 22 | var audioInput = null, 23 | realAudioInput = null, 24 | inputPoint = null, 25 | recording = false; 26 | var rafID = null; 27 | var analyserContext = null; 28 | var canvasWidth, canvasHeight; 29 | var socketio = io.connect(location.origin + '/audio', {transports: ['websocket']}); 30 | 31 | socketio.on('add-wavefile', function(url) { 32 | // add new recording to page 33 | audio = document.createElement('p'); 34 | audio.innerHTML = '