Fastify example
The example assumes you've already installed our SDK and you're ready to go.
If you wish to see more general info, visit set up your server article.
Load environment variables to Fastify
Use @fastify/env
package to load and set environment variables in your Fastify instance.
import
Fastify from "fastify"; importfastifyEnv from "@fastify/env"; constfastify =Fastify (); constenvSchema = {type : "object",required : ["FISHJAM_URL", "FISHJAM_MANAGEMENT_TOKEN"],properties : {FISHJAM_URL : {type : "string", },FISHJAM_MANAGEMENT_TOKEN : {type : "string", }, }, }; awaitfastify .register (fastifyEnv , {schema :envSchema });fastify .listen ({port : 3000 });
Setup Fishjam Fastify plugin
Fastify allows you to create plugins to encapsulate functionality in scopes. Check out fastify documentation for a deeper understanding of the concept.
First, extend the FastifyInstance
interface with fishjam
entry.
This will provide types of FishjamClient wherever you access fastify.fishjam
in the codebase.
Then, declare the plugin by invoking the fp
function with the setup function with as an argument.
import
fastifyPlugin from "fastify-plugin"; import {FishjamClient } from "@fishjam-cloud/js-server-sdk"; declare module "fastify" { interfaceFastifyInstance {fishjam :FishjamClient ;config : {FISHJAM_URL : string;FISHJAM_MANAGEMENT_TOKEN : string; }; } } export constfishjamPlugin =fastifyPlugin ((fastify ) => { constfishjamClient = newFishjamClient ({fishjamUrl :fastify .config .FISHJAM_URL ,managementToken :fastify .config .FISHJAM_MANAGEMENT_TOKEN , });fastify .decorate ("fishjam",fishjamClient ); });
Now, after registering the plugin, we will be able to use Fishjam client by accessing the fastify.fishjam
property.
await
fastify .register (fishjamPlugin );fastify .get ("/rooms", () =>fastify .fishjam .getAllRooms ());fastify .listen ({port : 3000 });
Listening to events
Fishjam instance is a stateful server that is emitting messages upon certain events. You can listen for those messages and react as you prefer. There are two options to obtain these.
Webhooks
To receive and parse the Fishjam protobuf messages, add a content type parser to your global (or scoped) Fastify instance.
Then, you will be able to access the parsed message at request.Body
.
import
Fastify , {FastifyRequest } from "fastify"; import {ServerMessage } from "@fishjam-cloud/js-server-sdk/proto"; constfastify =Fastify ();fastify .addContentTypeParser ( "application/x-protobuf", {parseAs : "buffer" }, async (_ :FastifyRequest ,body :Buffer ) =>ServerMessage .decode (newUint8Array (body )), );fastify .post <{Body :ServerMessage }>("/fishjam-webhook", (request ) => { // handle the messageconsole .log (request .body ); });
SDK Notifier
You can also leverage the Fastify plugin mechanism to use websockets to receive messages from Fishjam.
Let's create another plugin in fishjamNotifierPlugin.ts
file.
In this case, we don't need to extend the Fastify instance type, because the plugin doesn't decorate the Fastify instance with any properties. It will work in the background.
import { type
FastifyInstance } from "fastify"; importfp from "fastify-plugin"; import {FishjamWSNotifier } from "@fishjam-cloud/js-server-sdk"; export constfishjamNotifierPlugin =fp ((fastify ) => { constfishjamUrl =fastify .config .FISHJAM_URL ; constmanagementToken =fastify .config .FISHJAM_MANAGEMENT_TOKEN ; constfishjamNotifier = newFishjamWSNotifier ( {fishjamUrl ,managementToken }, (err ) =>fastify .log .error (err ), () =>fastify .log .info ("Websocket connection to Fishjam closed"), () =>fastify .log .error ("Failed to connect Fishjam notifier"), ); // handle the messages consthandleRoomCreated =console .log ; consthandlePeerAdded =console .log ;fishjamNotifier .on ("roomCreated",handleRoomCreated );fishjamNotifier .on ("peerAdded",handlePeerAdded ); });
Don't forget to register your plugin.
await
fastify .register (fishjamPlugin ); awaitfastify .register (fishjamNotifierPlugin );