Using Fluid with Microsoft Teams
This is a tutorial for integrating Fluid-powered real-time collaboration features into a Microsoft Teams tab application. By the end of this tutorial you will be able to integrate any Fluid-powered applications into Microsoft Teams and collaborate with others in real-time.
Concepts you will learn:
- How to integrate a Fluid client into a Microsoft Teams tab application
- How to run and connect your Teams application to a Fluid service (Azure Fluid Relay)
- How to create and get Fluid Containers and pass them to a React component
For an example of how this recipe may be used to build a more complex application, check out the Teams Fluid Hello World example in our FluidExamples repo.
Note
This tutorial assumes that you are familiar with the Fluid Framework Overview and that you have completed the QuickStart. You should also be familiar with the basics of React, React Hooks, and Microsoft Teams Tabs.
Create the project
-
Open a Command Prompt and navigate to the parent folder where you want to create the project, e.g.,
/My Microsoft Teams Projects
. -
Create a vanilla Teams tab application by running the below command. See the Microsoft Teams documentation for more information on setting up Teams applications.
yo teams
-
After creating the project, navigate to it with the command
cd <your project name>
. -
The project will use three Fluid libraries:
Library Description fluid-framework
Contains the IFluidContainer and other distributed data structures that synchronize data across clients. @fluidframework/azure-client
Defines the starting schema for the Fluid container. @fluidframework/test-client-utils
Defines the InsecureTokenProvider
needed to create the connection to a Fluid service.Run the following command to install the libraries.
npm install @fluidframework/azure-client fluid-framework @fluidframework/test-client-utils
Code the project
-
Open the file
/src/client/<your tab name>
in your code editor. Create a new file namedUtil.ts
. Then add the following import statements://`Util.ts import { IFluidContainer } from "fluid-framework"; import { AzureClient, AzureClientProps, LOCAL_MODE_TENANT_ID } from "@fluidframework/azure-client"; import { InsecureTokenProvider } from "@fluidframework/test-client-utils";
Defining Fluid functions and parameters
- Since this app is intended to be used in the context of Microsoft Teams, having all Fluid-related imports, initialization, and functions in one place makes it easier to use in the future. Add the following code below the import statements. The comments will define all the functions and constants needed to interact with the Fluid service and container.
// TODO 1: Define the parameter key(s).
// TODO 2: Define container schema.
// TODO 3: Define connectionConfig (AzureClientProps).
// TODO 4: Create Azure client.
// TODO 5: Define create container function.
// TODO 6: Define get container function.
- Replace
TODO 1:
with the following code. Note that the constant is being exported because it will be appended to thecontentUrl
in the Microsoft Teams settings and later for parsing the container ID in the content page. It’s a common pattern to store important query parameter keys as constants, rather than typing the raw string each time.
export const containerIdQueryParamKey = "containerId";
- Before the client can create any containers, it needs a
containerSchema
that will define the shared objects used in this application. This example uses a SharedMap as theinitialObjects
, but any shared object can be used. Note also thatmap
is the ID of theSharedMap
object and it must be unique within the container as with any other DDSes. ReplaceTODO: 2
with the following code.
const containerSchema = {
initialObjects: { map: SharedMap }
};
- Replace
TODO: 3
with the following code. Before the client can be used, it needs anAzureClientProps
that will define the type of connection the client will be using. Think of theconnectionConfig
as the properties required to connect to the service. Note that local mode of Azure Client is used here. To enable collaboration across all client, please replace it with Fluid Relay Service credentials. See how to set up the Azure Fluid Relay service for more information.
const connectionConfig : AzureClientProps =
{
connection: {
tenantId: LOCAL_MODE_TENANT_ID,
tokenProvider: new InsecureTokenProvider("foobar", { id: "user" }),
orderer: "http://localhost:7070",
storage: "http://localhost:7070"
}
};
- Replace
TODO: 4
with the following code.
const client = new AzureClient(connectionConfig);
- Replace
TODO: 5
with the following code. Note that since you are creating the container in the configuration page and appending it to thecontentUrl
in Microsoft Teams setting, you just need to return the container ID after attaching the container.
export async function createContainer() : Promise<string> {
const { container } = await client.createContainer(containerSchema);
const containerId = await container.attach();
return containerId;
};
- Replace
TODO: 6
with the following code. Note that when you fetch the Fluid container you want to return the container itself since your application will need to interact with the container and the DDSes inside it in the content page.
export async function getContainer(id : string) : Promise<IFluidContainer> {
const { container } = await client.getContainer(id, containerSchema);
return container;
};
Creating Fluid container in the configuration page
Open the file src/client/<your tab name>/<your tab name>Config.tsx
in your code editor. The standard Teams tab application flow goes from configuration to content page. To enable collaboration, persisting the container while loading into the content page is crucial. The best solution to persist the container is to append the container ID onto the contentUrl
and websiteUrl
, the URLs of the content page, as a query parameter. Since the save button in the Teams configuration page is the gateway between the configuration page and the content page, it is a great place to create the container and append the container ID in the settings.
- Add the following import statement.
import { createContainer, containerIdQueryParamKey } from "./Util";
- Replace the
onSaveHandler
method with the following code. Note that the only lines added here are calling the create container method defined earlier inUtils.ts
and then appending the returned container ID to thecontentUrl
andwebsiteUrl
as a query parameter.
1const onSaveHandler = async (saveEvent: microsoftTeams.settings.SaveEvent) => {
2 const host = "https://" + window.location.host;
3 const containerId = await createContainer();
4 microsoftTeams.settings.setSettings({
5 contentUrl: host + "/<your tab name>/?" + containerIdQueryParamKey + "=" + containerId + "&name={loginHint}&tenant={tid}&group={groupId}&theme={theme}",
6 websiteUrl: host + "/<your tab name>/?" + containerIdQueryParamKey + "=" + containerId + "&name={loginHint}&tenant={tid}&group={groupId}&theme={theme}",
7 suggestedDisplayName: "<your tab name>",
8 removeUrl: host + "/<your tab name>/remove.html?theme={theme}",
9 entityId: entityId.current
10 });
11 saveEvent.notifySuccess();
12};
Please make sure to replace <your tab name>
with the actual tab name from your project.
Warning
Since the content page URL is used to store the container ID, this record will be removed if the Teams tab is deleted. Additionally, every content page can only support one container ID.
Refactor content page to reflect Fluid application
Open the file src/client/<your tab name>/<your tab name>.tsx
in your code editor. A typical Fluid-powered application consists of a view and a Fluid data structure. Let’s just focus on getting/loading the Fluid container and leave all the Fluid related interactions in a React component.
- Add the following import statements in the content page.
import { IFluidContainer } from "fluid-framework";
import { getContainer, containerIdQueryParamKey } from "./Util";
- Now remove all the code below the import statements inside the content page and replace it with the following. Make sure to replace
<your tab name>
with the tab name you defined for your project.
export const <your tab name> = () => {
// TODO 1: Initialize Microsoft Teams.
// TODO 2: Initialize inTeams boolean.
// TODO 3: Define container as a React state.
// TODO 4: Define a method that gets the Fluid container
// TODO 5: Get Fluid container on content page startup.
// TODO 6: Pass the container to the React component as argument.
}
- Replace
TODO 1
with the following code. For the content page to display in Teams, you must include the Microsoft Teams JavaScript client SDK and include a call to initialize it after your page loads.
microsoftTeams.initialize();
- Replace
TODO 2
with the following code. Because the Teams application is just an IFrame injection of a webpage, you need to initialize theinTeams
boolean constant in order to know if the application is inside Microsoft Teams or not, and if the Teams resources, such as thecontentUrl
, are available.
const [{ inTeams }] = useTeams();
- Replace
TODO 3
with the following code. Using a React state for the container will provide the ability to dynmically update the container and the data objects inside it.
const [fluidContainer, setFluidContainer] = useState<IFluidContainer | undefined>(undefined);
- Replace
TODO 4
with the following code. Here you will parse the URL to get the query parameter string, defined bycontainerIdQueryParamKey
, and retrieve the container ID. With the container ID, you can now load the container to get the container. Once you have the container, set thefluidContainer
React state, defined above.
const getFluidContainer = async (url : URLSearchParams) => {
const containerId = url.get(containerIdQueryParamKey);
if (!containerId) {
throw Error("containerId not found in the URL");
}
const container = await getContainer(containerId);
setFluidContainer(container);
};
- Replace
TODO 5
with the following code. Now that you’ve defined how to get the Fluid container, you need to tell React to callgetFluidContainer
on load, and then store the result in state based on if the application is inside Teams. React’s useState hook will provide the storage needed, and useEffect will allow you to callgetFluidContainer
on render, passing the returned value intosetFluidContainer
.
By adding inTeams
in the dependency array at the end of the useEffect
, the app ensures that this function only gets called on content page load.
useEffect(() => {
if (inTeams === true) {
microsoftTeams.settings.getSettings(async (instanceSettings) => {
const url = new URL(instanceSettings.contentUrl);
getFluidContainer(url.searchParams);
});
microsoftTeams.appInitialization.notifySuccess();
}
}, [inTeams]);
- Replace
TODO 6
with the following code. Note here that it’s important to ensure that the content page is loaded inside Microsoft Teams and that the Fluid container is defined before passing it into the React component (defined asFluidComponent
below).
if (inTeams === false) {
return (
<div>This application only works in the context of Microsoft Teams</div>
);
}
if (fluidContainer !== undefined) {
return (
<FluidComponent fluidContainer={fluidContainer} />
);
}
return (
<div>Loading FluidComponent...</div>
);
Creating React component for Fluid view and data
Now that you have married the basic creation flow of Teams and Fluid, you can now create your own React component that handles the interactions between the application view and Fluid data. From this point on, the logic and flow behaves just like any other Fluid-powered application. With the basic structure established, you can create any of the Fluid examples as a Teams application by changing the ContainerSchema
and the application view’s interaction with the DDSes/data objects on the content page.
Start the Fluid server and run the application
If you are running your Teams application locally with Azure Client local mode, make sure to run the following command in the Command Prompt to start the Fluid service.
npx @fluidframework/azure-local-service@latest
To run and start the Teams application, open another terminal and follow the instructions to run the application server.
Now follow the instructions to upload the application to a Teams Tab.
Warning
Hostnames with ngrok
’s free tunnels are not preserved. Each run will generate a different URL. This means that anytime a new ngrok
tunnel is created, the older container will no longer be accessible. For production scenarios, please visit the section below
Next steps
Using AzureClient with Azure Fluid Relay
Because this is a Teams tab application, collaboration and interaction are the main focuses. Consider replacing the local mode AzureClientProps
provided above with non-local credentials from your Azure service instance, so others can join in and interact with you in the application! Check out how to provision your Azure Fluid Relay service.
Note
It is important to hide the credentials you are passing into AzureClientProps
from being accidentally committed to source control. The Teams project comes with a .env
file where you can store your credentials as environment variables and the file itself is already included in the .gitignore
. Refer to the section below if you want to use the environment variables in Teams.
Warning
InsecureTokenProvider
is a convenient way to test the application locally. It will be your respensibility to handle any user authentication and use a secure token for any production environment.
Setting and getting environment variable
To set a environment variable and retrieve it in Teams, you can take advantage of the built in .env
file. Set the environment variable in .env
like below.
# .env
TENANT_KEY=foobar
To pass the contents of the .env
file to our client-side app, you need to configure them into webpack.config.js
so that webpack
provides access to them at runtime. Add the environment variable from .env
as shown below.
// webpack,config.js
webpack.EnvironmentPlugin({
PUBLIC_HOSTNAME: undefined,
TAB_APP_ID: null,
TAB_APP_URI: null,
REACT_APP_TENANT_KEY: JSON.stringify(process.env.TENANT_KEY) // Add environment variable here
}),
Now, let’s access the environment variable in Util.ts
// Util.ts
tokenProvider: new InsecureTokenProvider(JSON.parse(process.env.REACT_APP_TENANT_KEY!), { id: "user" }),
Tip
When you make changes to the code the project will automatically rebuild and the application server will reload. However, if you make changes to the container schema, they will only take effect if you close and restart the application server. To do this, give focus to the Command Prompt and press Ctrl-C twice. Then run gulp serve
or gulp ngrok-serve
again.