v0.0.X

How to register your own signal in NextRTC

On GitHub I was asked how to implement own signal using NextRTC.

In configuration file you have to autowire SignalResolver as shown below:

@Autowired
private SignalResolver resolver;

then you have to add bean which will add your handlers. Handler has to implement only one interface SignalHandler. That interface has only one method so you can write it just like a inline lambda expression:

@Bean
public Signal addCustomNextRTCHandlers(){
    Signal upperCase = Signal.fromString("upperCase");
    resolver.addCustomHandler(upperCase, (msg)-> {
        InternalMessage.create()
                .to(msg.getFrom())
                .content(msg.getContent().toUpperCase())
                .signal(upperCase)
                .build()
                .send();
    });
    return upperCase;
}

In this example new handler (upperCase) will take a content of incomming message, and resend the content to sender with uppercase letters.

Whole configuration should like this:

@Configuration
@Import(NextRTCConfig.class)
public class EndpointConfig {

    @Autowired
    private SignalResolver resolver;

    @Bean
    public Signal addCustomNextRTCHandlers(){
        Signal upperCase = Signal.fromString("upperCase");
        resolver.addCustomHandler(upperCase, (msg)-> {
            InternalMessage.create()
                    .to(msg.getFrom())
                    .content(msg.getContent().toUpperCase())
                    .signal(upperCase)
                    .build()
                    .send();
        });
        return upperCase;
    }
}

On the js side you have to write two methods. One for sending new signal, and one which will react on the response:

NextRTC.prototype.upperCase = function upperCase(content, custom) {
    var nextRTC = this;
    nextRTC.request('upperCase', null, content, custom);
}; 
// this one will send content which will be handled by our handler


nextRTC.on('upperCase', content => {
// here you should handle the response
});

If you have great ideas for the future of NextRTC please leave a comment!

Leave a Reply