How to generate temp RTC token from agora that work with new package 'agora-rn-uikit'?

Viewed 31

I am using agora-rn-uikit agora-rn-uikit

And it works well with this code,

const App = () => {
  const [videoCall, setVideoCall] = useState(true);
  const connectionData = {
    appId: '<Agora App ID>',
    channel: 'test',
    rtcToken: "my token from agora"
  };
  const rtcCallbacks = {
    EndCall: () => setVideoCall(false),
  };
  return videoCall ? (
    <AgoraUIKit connectionData={connectionData} rtcCallbacks={rtcCallbacks} />
  ) : (
    <Text onPress={()=>setVideoCall(true)}>Start Call</Text>
  );
};

with this example, I can make a video chat app, and I get the token from my agora account, I just type a channel name and get a token that works, and everything is fine. but this is a temp token and can not depend on it in production, so I went to docs of agora and built a node js server to get me the token and the server works well and give me the token, but my problem is this token dosn't work and get a black screen on mobile app and error 110 in console.

and when I add a new key called "rtcUid" and give it a value "1" the video is working, but it works only locally not remotely, and I can't see the video of another phone,

here is my node js code, that i copied from agora docs

const express = require("express");
const { RtcTokenBuilder, RtcRole } = require("agora-access-token");
require("dotenv/config");

console.log(process.env.PORT);

const app = express();

const nocache = (_, resp, next) => {
  resp.header("Cache-Control", "private, no-cache, no-store, must-revalidate");
  resp.header("Expires", "-1");
  resp.header("Pragma", "no-cache");
  next();
};

const generateRTCToken = (req, resp) => {
  resp.header("Access-Control-Allow-Origin", "*");

  const channelName = req.params.channel;
  if (!channelName) {
    return resp.status(500).json({ error: "channel is required" });
  }

  let uid = req.params.uid;
  if (!uid || uid === "") {
    return resp.status(500).json({ error: "uid is required" });
  }
  // get role
  let role;
  if (req.params.role === "publisher") {
    role = RtcRole.PUBLISHER;
  } else if (req.params.role === "audience") {
    role = RtcRole.SUBSCRIBER;
  } else {
    return resp.status(500).json({ error: "role is incorrect" });
  }

  let expireTime = req.query.expiry;
  if (!expireTime || expireTime === "") {
    expireTime = 3600;
  } else {
    expireTime = parseInt(expireTime, 10);
  }

  const currentTime = Math.floor(Date.now() / 1000);
  const privilegeExpireTime = currentTime + expireTime;

  let token;
  if (req.params.tokentype === "userAccount") {
    token = RtcTokenBuilder.buildTokenWithAccount(
      process.env.APP_ID,
      process.env.APP_CERTIFICATE,
      channelName,
      uid,
      role,
      privilegeExpireTime
    );
  } else if (req.params.tokentype === "uid") {
    token = RtcTokenBuilder.buildTokenWithUid(
      process.env.APP_ID,
      process.env.APP_CERTIFICATE,
      channelName,
      uid,
      role,
      privilegeExpireTime
    );
  } else {
    return resp.status(500).json({ error: "token type is invalid" });
  }

  return resp.json({ rtcToken: token, channel: channelName });
};

app.get("/rtc/:channel/:role/:tokentype/:uid", nocache, generateRTCToken);

app.listen(process.env.PORT, () => {
  console.log(`Listening on port: ${process.env.PORT}`);
});

The token that works fine and solves my problem I get from agora when I enter to project config and select "Generate temp RTC token"

I need to know how to generate an agora rtcToken with node js that can work like that token I get from agora to use it with this new package agora-rn-uikit.

0 Answers
Related