Server Instance
Creating a Server
To start a Socket.IO server, create a Configuration, set the bind address and port, and then start the server.
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
SocketIOServer server = new SocketIOServer(config);When using frameworks such as Spring Boot, ensure you import com.socketio4j.socketio.Configuration and not framework classes with the same name (for example, org.springframework.context.annotation.Configuration), to avoid import conflicts.
Starting the Server
server.start()server.startAsync().addListener(future -> {
if (future.isSuccess()) {
System.out.println("Server started on " + config.getPort());
} else {
System.out.println("Error " + future.cause().getLocalizedMessage());
}
});Stopping the Server
Always stop the server gracefully during shutdown.
server.stop();Complete Example
Notes
hostnameis optional. If not set, the server binds to all interfaces (0.0.0.0/::0).portmust be set before starting the server.Threading, transports, and other advanced options can be customized via
Configurationbefore callingstart().
Last updated
Was this helpful?