openvidu-basic-node 🔗

Check it on GitHub

This is a minimal OpenVidu server application sample built for Node with Express. It internally uses openvidu-node-client SDK.

Running this application 🔗

Prerequisites 🔗

To run this application you will need Node installed on your system:

Download repository 🔗

git clone git@github.com:OpenVidu/openvidu-tutorials.git
cd openvidu-tutorials/openvidu-basic-node

Install dependencies 🔗

npm install

Run application 🔗

node index.js

Understanding the code 🔗

The application is a simple Express application with a single controller file index.js that exports two endpoints:

  • /api/sessions : Initialize a session.
  • /api/sessions/{{SESSION_ID}}/connections : Create a connection.

You can get more information about theses endpoints in the Application Server Endpoints section.

Let's see the code of the controller:

var cors = require("cors");
var app = express();

// Environment variable: PORT where the node server is listening
var SERVER_PORT = process.env.SERVER_PORT || 5000;
// Environment variable: URL where our OpenVidu server is listening
var OPENVIDU_URL = process.env.OPENVIDU_URL || 'http://localhost:4443';
// Environment variable: secret shared with our OpenVidu server
var OPENVIDU_SECRET = process.env.OPENVIDU_SECRET || 'MY_SECRET';

// Enable CORS support
app.use(
  cors({
    origin: "*",
  })
);

var server = http.createServer(app);
var openvidu = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET);

// Serve application
server.listen(SERVER_PORT, () => {
  console.log("Application started on port: ", SERVER_PORT);
  console.warn('Application server connecting to OpenVidu at ' + OPENVIDU_URL);
});

...

Starting by the top, the index.js file has the following fields:

  • cors: Allows the application to be accessed from any domain.
  • app: The Express application.
  • server: The HTTP server.
  • openvidu: The OpenVidu object that will be used to interact with the OpenVidu Server. It is initialized with the OPENVIDU_URL and OPENVIDU_SECRET environment variables.
  • SERVER_PORT: The port where the application will be listening.
  • OPENVIDU_URL: The URL where the OpenVidu Server is listening.
  • OPENVIDU_SECRET: The secret shared with the OpenVidu Server.


Initialize session endpoint 🔗

The first endpoint allows us initialize a new OpenVidu Session. The code of this endpoint is the following:

...

app.post("/api/sessions", async (req, res) => {
  var session = await openvidu.createSession(req.body);
  res.send(session.sessionId);
});

We build the Session object using the OpenVidu object and the parameters received from the request body. Finally, the Session ID is returned in the response body.


Create conneciton endpoint 🔗

The second and last endpoint has the goal of creating a new OpenVidu Connection in a session:

...

app.post("/api/sessions/:sessionId/connections", async (req, res) => {
  var session = openvidu.activeSessions.find(
    (s) => s.sessionId === req.params.sessionId
  );
  if (!session) {
    res.status(404).send();
  } else {
    var connection = await session.createConnection(req.body);
    res.send(connection.token);
  }
});

After checking if OpenVidu Session exists, the Connection object is built using the Session object and the parameters received from the request body.

Finally, the Token associated to the Connection is returned in the response body.