How to use nextrtc in java project

NextRTC is designed to be easy to integrate with existing solution.

First step is to add to your maven project dependency to latest version of nextrtc-signaling-server.


Then you have to expose your websocket channel, to allow js client to connect to server. In NextRTC you can do it by creating class:

import org.nextrtc.signalingserver.api.NextRTCEndpoint;
import org.nextrtc.signalingserver.codec.MessageDecoder;
import org.nextrtc.signalingserver.codec.MessageEncoder;

import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/signaling",//
        decoders = MessageDecoder.class,//
        encoders = MessageEncoder.class)
public class MyEndpoint extends NextRTCEndpoint {

Here you can read more about ServerEndpoint annotation.

In next step you have to import NextRTC configuration for example by adding following annotation in your java configuration class:

import org.nextrtc.signalingserver.NextRTCConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

class MyConfigClass {}

Now you are ready to handle events coming from signaling server.

The easiest way to do it is by creating class which will write some data to your db, or run some business method. To write handler you have to write class with following annotation:

import org.nextrtc.signalingserver.api.NextRTCHandler;
import org.nextrtc.signalingserver.api.annotation.NextRTCEventListener;
import org.nextrtc.signalingserver.api.dto.NextRTCEvent;
import org.springframework.stereotype.Component;

import static org.nextrtc.signalingserver.api.NextRTCEvents.UNEXPECTED_SITUATION;

public class ExceptionHandler implements NextRTCHandler {

    public void handleEvent(NextRTCEvent nextRTCEvent) {
        // do something with event

As you can see two required things is to implement interface NextRTCHandler and add annotation @NextrRTCEventListener(with event which should be handled by this class)

Example of use you can see in project (handle session opened, or other handlers)

Classes used in this doc are taken from project mentioned above.

Leave a Reply