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.
importFastify from "fastify"; importfastifyEnv from "@fastify/env"; constfastify =Fastify (); constenvSchema = {type : "object",required : ["FISHJAM_ID", "FISHJAM_MANAGEMENT_TOKEN"],properties : {FISHJAM_ID : {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.
importfastifyPlugin from "fastify-plugin"; import {FishjamClient } from "@fishjam-cloud/js-server-sdk"; declare module "fastify" { interfaceFastifyInstance {fishjam :FishjamClient ;config : {FISHJAM_ID : string;FISHJAM_MANAGEMENT_TOKEN : string; }; } } export constfishjamPlugin =fastifyPlugin ((fastify ) => { constfishjamClient = newFishjamClient ({fishjamId :fastify .config .FISHJAM_ID ,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.
awaitfastify .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.
importFastify , {FastifyRequest } from "fastify"; import {ServerMessage } from "@fishjam-cloud/js-server-sdk"; 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 { typeFastifyInstance } from "fastify"; importfp from "fastify-plugin"; import {FishjamWSNotifier } from "@fishjam-cloud/js-server-sdk"; export constfishjamNotifierPlugin =fp ((fastify ) => { constfishjamId =fastify .config .FISHJAM_ID ; constmanagementToken =fastify .config .FISHJAM_MANAGEMENT_TOKEN ; constfishjamNotifier = newFishjamWSNotifier ( {fishjamId ,managementToken }, (err ) =>fastify .log .error (err ), () =>fastify .log .info ("Websocket connection to Fishjam closed"), ); // handle the messages consthandleRoomCreated =console .log ; consthandlePeerAdded =console .log ;fishjamNotifier .on ("roomCreated",handleRoomCreated );fishjamNotifier .on ("peerAdded",handlePeerAdded ); });
Don't forget to register your plugin.
awaitfastify .register (fishjamPlugin ); awaitfastify .register (fishjamNotifierPlugin );