WebRTC Signaling Server Example Using Spring Boot
In this article, I will explain about building a simple Signaling Server to be used in WebRTC Chat application. I have not found many article about this subject, so I hope that this article will help you to get a basic understanding of signaling operation in WebRTC Chat Application.

Problem statements
- Signaling server for WebRTC Chat Application
Solution
The solution in here is using WebSocket technology. I used Spring Boot to create WebSocket server in short amount of time.
From business process point of view, we will implement the WebSocket server to handle these kind of signals:
- Login
- New Member
- Answer
- Offer
- ICE

From the above figure, we can see that the Login Activity mainly dealt on generating userId, and send the userId back to the WebSocket client.

From the above figure, we can see that the NewMember Activity mainly dealt on broadcasting the userId of the new member to other WebSocket clients.

From the above figure, we can see that the ICE/Answer/Offer Activity mainly dealt with forwarding the signal from sender to its respective recipient.
From technical point of view, we will need to add these dependency in maven:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
The signal data exchanged with WebSocket Client will use JSON format. The JSON consist of 4 field:
- userId: UserId that sent the data
- type (mandatory): The type of signal (Login, New Member, Answer,Offer,ICE, UserId)
- data: Data to be sent, it can be an ICE packet,Offer Packet, Answer Packet, etc
- toUid: UserId destination of data to be sent
We need to create Enumeration for Signal Type like below:
public enum SignalType {
Login,
UserId,
Offer,
Answer,
Ice,
NewMember,
}
We need to create a Spring Boot Application with its necessary annotation:
@SpringBootApplication
public class SignallingServerApplication {
public static void main(String[] args) {
SpringApplication.run(SignallingServerApplication.class, args);
}
}
We also need to implement the WebSocket configurer like below. Use allowed-origin to limit the domain that can call the Web Socket server.
@Value( "${allowed.origin:*}" )
private String allowedOrigin;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new SignalingHandler(), "/socket1").setAllowedOrigins(allowedOrigin);
}
We also need to implement the TextWebSocketHandler, and create a flow that matches our business process flow we’ve designed before. The complete code can be seen in this github (please don’t forget to leave a star).
For the complete flow of the communication between the browser and Signaling server, please see this article.
You can also check the working example of WebRTC Chat application on this link.