Skip to content

Commit 308d7f4

Browse files
committed
simplify code, make notifier work with tracks added
1 parent 353b166 commit 308d7f4

File tree

6 files changed

+150
-463
lines changed

6 files changed

+150
-463
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Selective Subscription Demo
2+
3+
Demo application showing selective subscription functionality with [Fishjam](https://fishjam.io) and the Python Server SDK.
4+
5+
## Prerequisites
6+
7+
- Python 3.11+
8+
- [uv](https://docs.astral.sh/uv/) package manager
9+
- Fishjam credentials ([get them here](https://fishjam.io/app))
10+
11+
## Quick Start
12+
13+
1. Install dependencies(in project root):
14+
```bash
15+
uv sync
16+
```
17+
18+
2. Run the server:
19+
```bash
20+
FISHJAM_ID=<your-id> \
21+
FISHJAM_MANAGEMENT_TOKEN=<your-token> \
22+
uv run examples/selective_subscription/main.py
23+
```
24+
25+
3. Open http://localhost:8000 in your browser
26+
27+
## Usage
28+
29+
1. Create peers with names
30+
2. Copy peer tokens and use them with a WebRTC client (e.g., [minimal-react](https://github.com/fishjam-cloud/web-client-sdk/tree/main/examples/react-client/minimal-react))
31+
3. Once peers have tracks, manage subscriptions through the web interface
32+
33+
### API Endpoints
34+
35+
- `POST /api/peers` - Create a peer with manual subscription mode
36+
- `POST /api/subscribe_peer` - Subscribe to all tracks from a peer
37+
- `POST /api/subscribe_tracks` - Subscribe to specific track IDs
38+

examples/selective_subscription/main.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,12 @@ async def lifespan(app):
1212
async with async_worker() as worker:
1313
notification_handler = NotificationHandler(room_service)
1414
worker.run_in_background(notification_handler.start())
15-
1615
print(f"Selective subscription demo started on http://{HOST}:{PORT}")
17-
print("Available endpoints:")
18-
print(" POST /api/peers - Create a new peer")
19-
print(" POST /api/subscribe_peer - subscribe to all tracks of a peer")
20-
print(" POST /api/subscribe_tracks - subscribe to specific tracks")
21-
2216
yield
2317

2418

2519
app.router.lifespan_context = lifespan
2620

2721

2822
if __name__ == "__main__":
29-
uvicorn.run(
30-
"main:app",
31-
host=HOST,
32-
port=PORT,
33-
reload=True,
34-
log_level="info"
35-
)
23+
uvicorn.run("main:app", host=HOST, port=PORT, reload=True, log_level="info")

examples/selective_subscription/selective_subscription/app.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
21
from pathlib import Path
3-
from typing import Dict, Any
42

53
from starlette.applications import Starlette
64
from starlette.middleware import Middleware
@@ -12,7 +10,6 @@
1210

1311
from .room_service import RoomService
1412

15-
1613
room_service = RoomService()
1714
templates = Jinja2Templates(directory=str(Path(__file__).resolve().parent.parent / "templates"))
1815

@@ -37,7 +34,6 @@ async def create_peer(request: Request) -> Response:
3734
"room_name": room_name,
3835
"peer_name": peer_name
3936
})
40-
4137
except Exception as e:
4238
return JSONResponse({"error": str(e)}, status_code=500)
4339

@@ -54,12 +50,11 @@ async def subscribe_peer(request: Request) -> Response:
5450
)
5551

5652
room_service.subscibe_peer(peer_id, target_peer_id)
57-
5853
return JSONResponse({"status": "subscribed"})
59-
6054
except Exception as e:
6155
return JSONResponse({"error": str(e)}, status_code=500)
62-
56+
57+
6358
async def subscribe_tracks(request: Request) -> Response:
6459
try:
6560
body = await request.json()
@@ -73,23 +68,16 @@ async def subscribe_tracks(request: Request) -> Response:
7368
)
7469

7570
room_service.subscribe_tracks(peer_id, track_ids)
76-
7771
return JSONResponse({"status": "subscribed"})
78-
7972
except Exception as e:
8073
return JSONResponse({"error": str(e)}, status_code=500)
8174

82-
async def health_check(request: Request) -> Response:
83-
return JSONResponse({"status": "OK"})
84-
85-
8675
async def serve_index(request: Request) -> Response:
8776
return templates.TemplateResponse("index.html", {"request": request})
8877

8978

9079
routes = [
9180
Route("/", serve_index, methods=["GET"]),
92-
Route("/health", health_check, methods=["GET"]),
9381
Route("/api/peers", create_peer, methods=["POST"]),
9482
Route("/api/subscribe_peer", subscribe_peer, methods=["POST"]),
9583
Route("/api/subscribe_tracks", subscribe_tracks, methods=["POST"]),

examples/selective_subscription/selective_subscription/notification_handler.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313

1414
class NotificationHandler:
15-
1615
def __init__(self, room_service: RoomService):
1716
self.room_service = room_service
1817
self._notifier = FishjamNotifier(FISHJAM_ID, FISHJAM_TOKEN)
18+
1919
@self._notifier.on_server_notification
2020
async def _(notification: AllowedNotification):
2121
match notification:
@@ -27,13 +27,9 @@ async def _(notification: AllowedNotification):
2727
peer_type=ServerMessagePeerType.PEER_TYPE_WEBRTC,
2828
):
2929
await handle_peer_disconnected(notification)
30-
case ServerMessageTrackAdded(
31-
peer_type=ServerMessagePeerType.PEER_TYPE_WEBRTC,
32-
):
30+
case ServerMessageTrackAdded():
3331
await handle_track_added(notification)
34-
case ServerMessageTrackRemoved(
35-
peer_type=ServerMessagePeerType.PEER_TYPE_WEBRTC,
36-
):
32+
case ServerMessageTrackRemoved():
3733
await handle_track_removed(notification)
3834

3935
async def handle_peer_connected(notification: ServerMessagePeerConnected):
@@ -47,9 +43,7 @@ async def handle_track_added(notification: ServerMessageTrackAdded):
4743

4844
async def handle_track_removed(notification: ServerMessageTrackRemoved):
4945
print(f"Track removed: {notification.track}")
50-
46+
5147
async def start(self) -> None:
52-
"""Long-running coroutine that connects the notifier and processes messages."""
5348
await self._notifier.connect()
54-
5549

examples/selective_subscription/selective_subscription/room_service.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@
77

88

99
class RoomService:
10-
1110
def __init__(self):
12-
self.fishjam = FishjamClient(
13-
FISHJAM_ID,
14-
FISHJAM_TOKEN,
15-
)
11+
self.fishjam = FishjamClient(FISHJAM_ID, FISHJAM_TOKEN)
1612
self.room = self.fishjam.create_room(RoomOptions(
1713
max_peers=10,
1814
room_type="conference"
1915
))
20-
16+
2117
def get_or_create_room(self) -> Room:
2218
if self.room:
2319
try:
@@ -27,26 +23,19 @@ def get_or_create_room(self) -> Room:
2723
pass
2824

2925
return self.fishjam.create_room()
30-
26+
3127
def create_peer(self) -> tuple[Peer, str]:
3228
room = self.get_or_create_room()
33-
34-
options = PeerOptions(
35-
subscribe_mode="manual",
36-
)
37-
29+
options = PeerOptions(subscribe_mode="manual")
3830
peer, token = self.fishjam.create_peer(room.id, options)
3931
return peer, token
4032

41-
4233
def subscibe_peer(self, peer_id: str, target_peer_id: str):
4334
room = self.get_or_create_room()
44-
4535
self.fishjam.subscribe_peer(room.id, peer_id, target_peer_id)
46-
36+
4737
def subscribe_tracks(self, peer_id: str, track_ids: List[str]):
4838
room = self.get_or_create_room()
49-
5039
self.fishjam.subscribe_tracks(room.id, peer_id, track_ids)
5140

5241
def get_peer_session(self, peer_id: str):
@@ -59,4 +48,4 @@ def __init__(self):
5948

6049
return _Session()
6150

62-
return None
51+
return None

0 commit comments

Comments
 (0)