Tutorial: Backend Quick Start
Tutorial - Learn to build a backend for your Fishjam app
This tutorial will guide you through setting up a backend server that can create rooms and manage peers for your Fishjam application.
By the end, you'll have a working backend and understand how to integrate it with your client apps.
Choose your language
Select your preferred backend language and all code examples will be shown for that language:
- Node.js / TypeScript
- Python
We'll use Node.js with TypeScript and the @fishjam-cloud/js-server-sdk
package.
We'll use Python and the fishjam-server-sdk
package.
What you'll build
A simple backend server that can create rooms, add peers, and generate tokens for client apps.
What you'll learn
- How to install and set up a Fishjam server SDK
- How to create and manage rooms
- How to create peers and generate tokens
- How to listen for Fishjam events
Prerequisites
- Node.js / TypeScript
- Python
- Node.js environment set up
- Access to Fishjam Dashboard with your credentials
- Python environment set up
- Access to Fishjam Dashboard with your credentials
Step 1: Install the SDK
- Node.js / TypeScript
- Python
Install the JavaScript server SDK using your preferred package manager:
- npm
- yarn
- pnpm
npm install @fishjam-cloud/js-server-sdk
yarn add @fishjam-cloud/js-server-sdk
pnpm add @fishjam-cloud/js-server-sdk
Install the Python server SDK using your preferred package manager:
- pip
- poetry
pip install fishjam-server-sdk
poetry add fishjam-server-sdk
Step 2: Get your credentials
- Log in to Fishjam Dashboard
- Navigate to your app
- Copy your Fishjam URL and Management Token
- Store them as environment variables:
- Node.js / TypeScript
- Python
export FISHJAM_URL="your-fishjam-url" export FISHJAM_MANAGEMENT_TOKEN="your-management-token"
export FISHJAM_URL="your-fishjam-url" export FISHJAM_MANAGEMENT_TOKEN="your-management-token"
Step 3: Set up the client
Create your Fishjam client:
- Node.js / TypeScript
- Python
import {
FishjamClient } from '@fishjam-cloud/js-server-sdk'; constfishjamUrl =process .env .FISHJAM_URL ; constmanagementToken =process .env .FISHJAM_MANAGEMENT_TOKEN ; constfishjamClient = newFishjamClient ({fishjamUrl ,managementToken });
import os from fishjam import FishjamClient fishjam_url = os.environ["FISHJAM_URL"] management_token = os.environ["FISHJAM_MANAGEMENT_TOKEN"] fishjam_client = FishjamClient(fishjam_url, management_token)
Step 4: Create a room
Create your first room:
- Node.js / TypeScript
- Python
async function
createRoom () { constroom = awaitfishjamClient .createRoom ();console .log ('Room created:',room .id ); returnroom ; }
def create_room(): room = fishjam_client.create_room() print(f'Room created: {room.id}') return room
Step 5: Add a peer to the room
Create a peer and get a token for your client app:
- Node.js / TypeScript
- Python
async function
addPeer (roomId :RoomId ,peerName : string) { const {peer ,peerToken } = awaitfishjamClient .createPeer (roomId , {metadata : {name :peerName }, });console .log ('Peer created:',peer .id );console .log ('Peer token:',peerToken ); return {peer ,peerToken }; }
from fishjam import PeerOptions def add_peer(room_id: str, peer_name: str): options = PeerOptions(metadata={"name": peer_name}) peer, peer_token = fishjam_client.create_peer(room_id, options=options) print(f'Peer created: {peer.id}') print(f'Peer token: {peer_token}') return peer, peer_token
Step 6: Create a simple API endpoint
Build a simple HTTP endpoint that your client apps can call:
- Node.js / TypeScript
- Python
import
express from 'express'; import {FishjamClient } from '@fishjam-cloud/js-server-sdk'; constapp =express ();app .use (express .json ()); constfishjamClient = newFishjamClient ({fishjamUrl :process .env .FISHJAM_URL !,managementToken :process .env .FISHJAM_MANAGEMENT_TOKEN ! });app .post ('/join-room', async (req ,res ) => { try { const {roomName ,peerName } =req .body ; // Create room constroom = awaitfishjamClient .createRoom (); // Add peer const {peer ,peerToken } = awaitfishjamClient .createPeer (room .id , {metadata : {name :peerName }, });res .json ({roomId :room .id ,peerToken ,fishjamUrl :process .env .FISHJAM_URL }); } catch (error : any) {res .status (500).json ({error :error .message }); } });app .listen (3000, () => {console .log ('Server running on port 3000'); });
Don't forget to install Express:
- npm
- yarn
- pnpm
npm install express @types/express
yarn add express @types/express
pnpm add express @types/express
import os from fastapi import FastAPI, HTTPException from pydantic import BaseModel from fishjam import FishjamClient, PeerOptions app = FastAPI() fishjam_client = FishjamClient( os.environ["FISHJAM_URL"], os.environ["FISHJAM_MANAGEMENT_TOKEN"] ) class JoinRoomRequest(BaseModel): room_name: str peer_name: str @app.post("/join-room") async def join_room(request: JoinRoomRequest): try: # Create room room = fishjam_client.create_room() # Add peer options = PeerOptions(metadata={"name": request.peer_name}) peer, peer_token = fishjam_client.create_peer(room.id, options=options) return { "room_id": room.id, "peer_token": peer_token, "fishjam_url": os.environ["FISHJAM_URL"] } except Exception as e: raise HTTPException(status_code=500, detail=str(e))
Don't forget to install FastAPI:
- pip
- poetry
pip install fastapi[standard]
poetry add fastapi[standard]
Step 7: Run your server
Start your backend server:
- Node.js / TypeScript
- Python
# If using TypeScript directly npx tsx server.ts # Or compile and run npx tsc server.ts && node server.js
fastapi run --port 3000
Step 8: Test your backend
Test your endpoint:
- Node.js / TypeScript
- Python
curl -X POST http://localhost:3000/join-room \ -H "Content-Type: application/json" \ -d '{"roomName": "test-room", "peerName": "test-user"}'
curl -X POST http://localhost:3000/join-room \ -H "Content-Type: application/json" \ -d '{"room_name": "test-room", "peer_name": "test-user"}'
You should receive a response with:
roomId
/room_id
- The ID of the created roompeerToken
/peer_token
- Token for the client to join the roomfishjamUrl
/fishjam_url
- URL for the client to connect to
Complete example
Here's a complete working backend:
- Node.js / TypeScript
- Python
import
express from 'express'; import {FishjamClient } from '@fishjam-cloud/js-server-sdk'; constapp =express ();app .use (express .json ()); constfishjamClient = newFishjamClient ({fishjamUrl :process .env .FISHJAM_URL !,managementToken :process .env .FISHJAM_MANAGEMENT_TOKEN ! });app .post ('/join-room', async (req ,res ) => { try { const {roomName ,peerName } =req .body ; constroom = awaitfishjamClient .createRoom (); const {peer ,peerToken } = awaitfishjamClient .createPeer (room .id , {metadata : {name :peerName }, });res .json ({roomId :room .id ,peerToken ,fishjamUrl :process .env .FISHJAM_URL }); } catch (error : any) {res .status (500).json ({error :error .message }); } });app .listen (3000, () => {console .log ('Fishjam backend running on port 3000'); });
import os from fastapi import FastAPI, HTTPException from pydantic import BaseModel from fishjam import FishjamClient, PeerOptions app = FastAPI() fishjam_client = FishjamClient( os.environ["FISHJAM_URL"], os.environ["FISHJAM_MANAGEMENT_TOKEN"] ) class JoinRoomRequest(BaseModel): room_name: str peer_name: str @app.post("/join-room") async def join_room(request: JoinRoomRequest): try: room = fishjam_client.create_room() options = PeerOptions(metadata={"name": request.peer_name}) peer, peer_token = fishjam_client.create_peer(room.id, options=options) return { "room_id": room.id, "peer_token": peer_token, "fishjam_url": os.environ["FISHJAM_URL"] } except Exception as e: raise HTTPException(status_code=500, detail=str(e))
Next steps
Now that you have a working backend, explore these guides:
- How to set up a production deployment
- How to handle webhooks and events
- How to implement a FastAPI backend
- How to implement a Fastify backend
Or learn more about Fishjam concepts: