openvidu-basic-java 🔗

Check it on GitHub

This is a minimal OpenVidu server application sample built for Java with Spring Boot. It internally uses openvidu-java-client SDK.

Running this application 🔗

Prerequisites 🔗

To run this application you will need Java and Maven installed on your system:

Download repository 🔗

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

Run application 🔗

mvn spring-boot: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/{{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:

@CrossOrigin(origins = "*")
@RestController
public class Controller {

@Value("${OPENVIDU_URL}")
    private String OPENVIDU_URL;

    @Value("${OPENVIDU_SECRET}")
    private String OPENVIDU_SECRET;

    private OpenVidu openvidu;

    @PostConstruct
    public void init() {
        this.openvidu = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET);
    }

    ...
}

Starting by the top, the Controller class has the following annotations:

  • @CrossOrigin(origins = "*"): Allows the application to be accessed from any domain.
  • @RestController: Marks the class as a controller where every method returns a domain object instead of a view.

Going deeper, the Controller class has the following fields:

  • OPENVIDU_URL: The URL of the OpenVidu Server. It is injected from the environment variable OPENVIDU_URL using the @Value("${OPENVIDU_URL}") annotation.

  • OPENVIDU_SECRET: The URL of the OpenVidu Server. It is injected from the environment variable OPENVIDU_SECRET using the @Value("${OPENVIDU_SECRET}") annotation.

  • openvidu: The OpenVidu object that will be used to interact with the OpenVidu Server. It is initialized in the init() method which is executed after dependency injection is done to perform any initialization using the @PostConstruct annotation.


Initialize session endpoint 🔗

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

@CrossOrigin(origins = "*")
@RestController
public class Controller {
    ...

    /**
     * @param params The Session properties
     * @return The Session ID
     */
    @PostMapping("/api/sessions")
    public ResponseEntity<String> initializeSession(@RequestBody(required = false) Map<String, Object> params)
            throws OpenViduJavaClientException, OpenViduHttpException {
        SessionProperties properties = SessionProperties.fromJson(params).build();
        Session session = openvidu.createSession(properties);
        return new ResponseEntity<>(session.getSessionId(), HttpStatus.OK);
    }
}

We build the SessionProperties object using the parameters received from the request body. This property is necessary to configure the Session object.

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:

@CrossOrigin(origins = "*")
@RestController
public class Controller {
    ...

    /**
     * @param sessionId The Session in which to create the Connection
     * @param params    The Connection properties
     * @return The Token associated to the Connection
     */
    @PostMapping("/api/sessions/{sessionId}/connections")
    public ResponseEntity<String> createConnection(@PathVariable("sessionId") String sessionId,
            @RequestBody(required = false) Map<String, Object> params)
            throws OpenViduJavaClientException, OpenViduHttpException {
        Session session = openvidu.getActiveSession(sessionId);
        if (session == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        ConnectionProperties properties = ConnectionProperties.fromJson(params).build();
        Connection connection = session.createConnection(properties);
        return new ResponseEntity<>(connection.getToken(), HttpStatus.OK);
    }
}

After checking if OpenVidu Session exists, we build the ConnectionProperties object from the request body. After that, we uses the Session object, which has been retrieved from the OpenVidu object using the sessionId path variable, for creating a new Connection object.

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