Skip to content

Instantly share code, notes, and snippets.

@AdnanKhan45
Last active January 21, 2024 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdnanKhan45/b4da97865439c5b36e717f00365eb6d1 to your computer and use it in GitHub Desktop.
Save AdnanKhan45/b4da97865439c5b36e717f00365eb6d1 to your computer and use it in GitHub Desktop.

Setup Node Server

To setup a node server create an empty directory and open it in VS Code or any editor and open Terminal and run:

$ npm init

Install Dependencies

Install some necessary dependencies:

$ npm install express

and

$ npm install agora-access-token

Once you have the following dependencies in your package.json. Then, create a file in the project name server.js and paste the code in it.

Run Server

To run the node server you need to run:

node server.js

Once you get the log in the console Agora token server running on http://localhost:3000 then you're good to go in setting up your node server.

Where and How to deal with tokenUrl?

If your Flutter app is running on virtual or a physical device, you need to copy your IP Address of the internet you're currently connected with and the port number which in our case is 3000.

Your tokenUrl should look something like this for using it with AgoraClient.

"http://XXX.XX.XX.X:3000/get_token?channelName=${YOUR-CHANNEL-NAME}"

const express = require('express');
const { RtcTokenBuilder, RtcRole } = require('agora-access-token');
const app = express();
const port = 3000; // You can use any port
// Agora Credentials
const appId = 'REPLACE-HERE-YOUR-AGORA-APP-ID';
const appCertificate = 'REPLACE-HERE-YOUR-AGORA-APP-CERTIFICATE';
app.get('/get_token', (req, res) => {
const channelName = req.query.channelName;
if (!channelName) {
return res.status(400).json({ 'error': 'channelName is required' });
}
// Token expiration time
const expirationTimeInSeconds = 3600;
const currentTimestamp = Math.floor(Date.now() / 1000);
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds;
// Assign a role to the user
const role = RtcRole.PUBLISHER;
const token = RtcTokenBuilder.buildTokenWithUid(appId, appCertificate, channelName, 0, role, privilegeExpiredTs);
res.json({ 'token': token });
});
app.listen(port, () => {
console.log(`Agora token server running on http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment