Giter VIP home page Giter VIP logo

agora-token-server-nodejs's Introduction

agora-token-server-in-Express

To generate agora token, we need a simple express application.

$ npm init -y

This will initialize a basic package.json file. Now, install the dependecies.

$ npm install express agora-token-generator dot-env

Now, create a basic express application. In server.js file put the following code.

const express = require("express");
const app = express();

require("dotenv").config();

app.get("/", (req, res) => {
    res.sendStatus(200);
});

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
    console.log(`App is running on port ${PORT}`);
});

Now, we will test the server in localhost. Run,

$ node server.js

Our server is running on PORT 5000. If we ping localhost:5000, we will get a 200 OK status. Now, import agora-token-generator on our server.

const {
    RtmTokenBuilder,
    RtmRole,
    RtcTokenBuilder,
    RtcRole,
} = require("agora-access-token");

Finally add some route for different tokens. Here I create some route for tokens.

const generateRtmToken = (account) => {
    const appID = process.env.appID;
    const appCertificate = process.env.appCertificate;

    const expirationTimeInSeconds = 3600;
    const currentTimestamp = Math.floor(Date.now() / 1000);

    const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds;

    return RtmTokenBuilder.buildToken(
        appID,
        appCertificate,
        account,
        RtmRole,
        privilegeExpiredTs
    );
};

const generateRtcToken = (account, channelName = "testChannel") => {
    const appID = process.env.appID;
    const appCertificate = process.env.appCertificate;

    const expirationTimeInSeconds = 3600;
    const currentTimestamp = Math.floor(Date.now() / 1000);

    const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds;

    return RtcTokenBuilder.buildTokenWithAccount(
        appID,
        appCertificate,
        channelName,
        account,
        RtcRole,
        privilegeExpiredTs
    );
};

const generateRtcUidToken = (uid, channelName = "testChannel") => {
    const appID = process.env.appID;
    const appCertificate = process.env.appCertificate;

    const expirationTimeInSeconds = 3600;
    const currentTimestamp = Math.floor(Date.now() / 1000);

    const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds;

    const rtcUidToken = RtcTokenBuilder.buildTokenWithUid(
        appID,
        appCertificate,
        channelName,
        uid,
        RtcRole,
        privilegeExpiredTs
    );

    return rtcUidToken;
};

app.get("/rtc-uid-token", (req, res) => {
    const uid = req.query.uid;
    const channelName = req.query.channelName;
    const rtcUidToken = generateRtcUidToken(uid, channelName);
    res.status(200).json({ token: rtcUidToken });
});
app.get("/rtm-token", (req, res) => {
    const account = req.query.username;
    const rtmToken = generateRtmToken(account);
    res.status(200).json({ token: rtmToken });
});
app.get("/rtc-token", (req, res) => {
    const account = req.query.username;
    const channelName = req.query.channelName;
    const rtcToken = generateRtcToken(account, channelName);
    res.status(200).json({ token: rtcToken });
});

For ping different route to get tokens.

agora-token-server-nodejs's People

Contributors

aljubaer avatar moshiuzzaman avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.