0% found this document useful (0 votes)
24 views3 pages

Video Call App with Flask & React

Uploaded by

thearyaanthaakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Video Call App with Flask & React

Uploaded by

thearyaanthaakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

from flask import Flask, render_template, request, jsonify

import uuid

app = Flask(__name__)

# In-memory storage for rooms


rooms = {}

@[Link]('/create_room', methods=['POST'])
def create_room():
room_id = str(uuid.uuid4())
rooms[room_id] = []
return jsonify({'room_id': room_id})

@[Link]('/join_room/<room_id>', methods=['POST'])
def join_room(room_id):
if room_id in rooms:
user_id = str(uuid.uuid4())
rooms[room_id].append(user_id)
return jsonify({'user_id': user_id})
return jsonify({'error': 'Room not found'}), 404

@[Link]('/get_room/<room_id>', methods=['GET'])
def get_room(room_id):
if room_id in rooms:
return jsonify({'users': rooms[room_id]})
return jsonify({'error': 'Room not found'}), 404

if __name__ == '__main__':
[Link](debug=True)
npx create-react-app video-call-app
cd video-call-app
npm start
import React, { useState, useRef, useEffect } from 'react';
import io from '[Link]-client';

const App = () => {


const [roomId, setRoomId] = useState('');
const [userId, setUserId] = useState('');
const [peerConnections, setPeerConnections] = useState({});
const videoRef = useRef();

const createRoom = async () => {


const response = await fetch('/create_room', { method: 'POST' });
const data = await [Link]();
setRoomId(data.room_id);
};

const joinRoom = async (roomId) => {


const response = await fetch(`/join_room/${roomId}`, { method: 'POST' });
const data = await [Link]();
if ([Link]) {
alert([Link]);
} else {
setUserId(data.user_id);
}
};

useEffect(() => {
if (userId) {
const socket = [Link]('/');
[Link]('join', { roomId, userId });

[Link]('offer', async (data) => {


const peerConnection = new RTCPeerConnection();
peerConnections[[Link]] = peerConnection;
setPeerConnections({ ...peerConnections });

[Link] = (event) => {


if ([Link]) {
[Link]('candidate', { to: [Link], candidate:
[Link] });
}
};

[Link] = (event) => {


[Link] = [Link][0];
};

await [Link](new
RTCSessionDescription([Link]));
const answer = await [Link]();
await [Link](answer);

[Link]('answer', { to: [Link], answer });


});

[Link]('answer', async (data) => {


const peerConnection = peerConnections[[Link]];
await [Link](new
RTCSessionDescription([Link]));
});

[Link]('candidate', async (data) => {


const peerConnection = peerConnections[[Link]];
await [Link](new RTCIceCandidate([Link]));
});

[Link]({ video: true, audio: true })


.then(stream => {
[Link] = stream;
[Link]().forEach(track => {
[Link](peerConnections).forEach(peerConnection => {
[Link](track, stream);
});
});
});
}
}, [userId]);

return (
<div>
<button onClick={createRoom}>Create Room</button>
<input type="text" placeholder="Room ID" value={roomId} onChange={(e) =>
setRoomId([Link])} />
<button onClick={() => joinRoom(roomId)}>Join Room</button>
<video ref={videoRef} autoPlay></video>
</div>
);
};

export default App;from flask_socketio import SocketIO, join_room, leave_room, emit

socketio = SocketIO(app)

@[Link]('join')
def handle_join(data):
room_id = data['roomId']
user_id = data['userId']
join_room(room_id)
emit('user_joined', {'userId': user_id}, room=room_id)

@[Link]('offer')
def handle_offer(data):
room_id = data['roomId']
emit('offer', data, room=room_id)

@[Link]('answer')
def handle_answer(data):
room_id = data['roomId']
emit('answer', data, room=room_id)

@[Link]('candidate')
def handle_candidate(data):
room_id = data['roomId']
emit('candidate', data, room=room_id)

if __name__ == '__main__':
[Link](app, debug=True)

You might also like