openvidu-basic-dotnet 🔗

Check it on GitHub

This is a minimal OpenVidu server application sample built for Python with .NET with ASP.NET Core Minimal APIs. It internally uses the OpenVidu REST API.

Running this application 🔗

Prerequisites 🔗

To run this application you will need .NET:

Download repository 🔗

git clone https://github.com/OpenVidu/openvidu-tutorials.git -b v2.29.0
cd openvidu-tutorials/openvidu-basic-dotnet

Run application 🔗

dotnet run

Understanding the code 🔗

The application is a simple Spring Boot application with a single controller class Controller.java that exports two endpoints:

  • /api/sessions : Initialize a session.
  • /api/sessions/:sessionId/connections : Create a connection.

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

Let's see the code of the controller:

var builder = WebApplication.CreateBuilder(args);
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

IConfiguration config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddEnvironmentVariables().Build();

// Load env variables
var SERVER_PORT = config.GetValue<int>("SERVER_PORT");
var OPENVIDU_URL = config.GetValue<string>("OPENVIDU_URL");
var OPENVIDU_SECRET = config.GetValue<string>("OPENVIDU_SECRET");

// Enable CORS support
builder.Services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
                      builder =>
                      {
                          builder.WithOrigins("*").AllowAnyHeader();
                      });
});

builder.WebHost.UseKestrel(serverOptions => {
    serverOptions.ListenAnyIP(SERVER_PORT);
});

var app = builder.Build();
app.UseCors(MyAllowSpecificOrigins);


// Allow for insecure certificate in OpenVidu deployment
var handler = new HttpClientHandler
{
    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
HttpClient client = new HttpClient(handler);
client.BaseAddress = new System.Uri(OPENVIDU_URL);

Starting by the top, the Program.cs file has the following fields:

  • builder: a WebApplicationBuilder instance to build the application.
  • config: a IConfiguration instance to load the configuration from the appsettings.json file.
  • client: a HttpClient instance to make HTTP requests to the OpenVidu REST API.
  • MyAllowSpecificOrigins: the name of the CORS policy to be used in the application.
  • app: the WebApplication instance.
  • SERVER_PORT: the port where the application will be listening.
  • OPENVIDU_URL: the URL of your OpenVidu deployment.
  • OPENVIDU_SECRET: the secret of your OpenVidu deployment.

The first thing the application does is to configure CORS support. The CORS policy is configured to allow requests from any origin and any header.


Initialize session endpoint 🔗

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

// Set OpenVidu deployment secret
var basicAuth = Convert.ToBase64String(System.Text.ASCIIEncoding.UTF8.GetBytes($"OPENVIDUAPP:{OPENVIDU_SECRET}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);

app.MapPost("/api/sessions", async (HttpRequest request) =>
{
    String contentString;
    HttpContent content;
    using (var streamContent = new StreamContent(request.Body)) {
        contentString = await streamContent.ReadAsStringAsync();
        content = new StringContent(contentString, Encoding.UTF8, "application/json");
    }
    HttpResponseMessage response = await client.PostAsync("openvidu/api/sessions", content);
    if (response.StatusCode == HttpStatusCode.Conflict) {
        // Session already exists in OpenVidu
        var bodyRequest = JsonSerializer.Deserialize<Dictionary<string, object>>(contentString);
        return bodyRequest["customSessionId"];
    }
    response.EnsureSuccessStatusCode();
    var responseBody = await response.Content.ReadFromJsonAsync<Dictionary<string, object>>();
    var sessionId = responseBody["sessionId"].ToString().Trim('"');
    return sessionId;
});

The endpoint creates a new Session using the OpenVidu REST API and returns the sessionId of the new session. If the request brought a customSessionId parameter and that session already existed in the OpenVidu deployment (that's the 409 CONFLICT error), then the endpoint simply returns the same customSessionId. At this point the Session is ready to create Connections, whether it is a newly created Session or an already existing one.


Create conneciton endpoint 🔗

The second endpoint allows us to create a new OpenVidu Connection in the session:

app.MapPost("/api/sessions/{sessionId}/connections", async (HttpRequest request, [FromRoute] string sessionId) =>
{
    HttpContent content;
    using (var streamContent = new StreamContent(request.Body)) {
        var contentString = await streamContent.ReadAsStringAsync();
        content = new StringContent(contentString, Encoding.UTF8, "application/json");
    }
    HttpResponseMessage response = await client.PostAsync("openvidu/api/sessions/" + sessionId.Trim('"') + "/connection", content);
    response.EnsureSuccessStatusCode();
    var responseBody = await response.Content.ReadFromJsonAsync<Dictionary<string, object>>();
    var token = responseBody["token"].ToString().Trim('"');
    return token;
});

The endpoint creates a new Connection using the OpenVidu REST API and returns the token of the new connection. We can use this token in openviu-browser SDK to connect the user to the Session.