openvidu-basic-python 🔗
This is a minimal OpenVidu server application sample built for Python with Flask. It internally uses the OpenVidu REST API.
Running this application 🔗
Prerequisites 🔗
To run this application you will need Python 3 installed on your system:
Download repository 🔗
git clone git@github.com:OpenVidu/openvidu-tutorials.git
cd openvidu-tutorials/openvidu-basic-python
Create a python3 environment and activate it 🔗
python3 -m venv venv
. venv/bin/activate
Install dependencies 🔗
pip install -r requirements.txt
Run application 🔗
python3 app.py
Understanding the code 🔗
The application is a simple Flask application with a single controller file app.py
that exports two endpoints:
/api/sessions
: Initialize a session./api/sessions/{{SESSION_ID}}/connections
: Create a connection.
You can get more information about theses endpoints in the Application Server Endpoints section.
Let's see the code of the controller:
app = Flask(__name__)
# Enable CORS support
cors = CORS(app, resources={r"/*": {"origins": "*"}})
# Load env variables
SERVER_PORT = os.environ.get("SERVER_PORT")
OPENVIDU_URL = os.environ.get("OPENVIDU_URL")
OPENVIDU_SECRET = os.environ.get("OPENVIDU_SECRET")
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=SERVER_PORT)
...
Starting by the top, the app.py
file has the following fields:
app
: Flask application.cors
: CORS support for Flask application.SERVER_PORT
: Port of the application server.OPENVIDU_URL
: URL of the OpenVidu Server.OPENVIDU_SECRET
: Secret of the OpenVidu Server.
Initialize session endpoint 🔗
The first endpoint allows us initialize a new OpenVidu Session. The code of this endpoint is the following:
...
@app.route("/api/sessions", methods=['POST'])
def initializeSession():
try:
body = request.json if request.data else {}
response = requests.post(
OPENVIDU_URL + "openvidu/api/sessions",
verify=False,
auth=("OPENVIDUAPP", OPENVIDU_SECRET),
headers={'Content-type': 'application/json'},
json=body
)
response.raise_for_status()
return response.json()["sessionId"]
except requests.exceptions.HTTPError as err:
if (err.response.status_code == 409):
# Session already exists in OpenVidu
return request.json["customSessionId"]
else:
return err
The endpoint receives a POST request with a JSON body containing the customSessionId
of the session to be created. If the session already exists in OpenVidu, the endpoint returns the customSessionId
received in the request body. Otherwise, it creates a new session, using the
OpenVidu REST API and returns the sessionId
of the new session.
Create conneciton endpoint 🔗
The second and last endpoint has the goal of creating a new OpenVidu Connection in a session:
...
@app.route("/api/sessions/<sessionId>/connections", methods=['POST'])
def createConnection(sessionId):
body = request.json if request.data else {}
return requests.post(
OPENVIDU_URL + "openvidu/api/sessions/" + sessionId + "/connection",
verify=False,
auth=("OPENVIDUAPP", OPENVIDU_SECRET),
headers={'Content-type': 'application/json'},
json=body
).json()["token"]
The endpoint creates a new Connection using the OpenVidu REST API and returns the token
of the new connection.