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);

Starting the Server

server.start()

Stopping the Server

Always stop the server gracefully during shutdown.

server.stop();

socketio4j adds shutdown hook after the server started, it gracefully stops the server in certain scenarios, not all. It is always recommended to stop the server explicitly.

Complete Example

Check Events for event handling related documentation

Notes

  • hostname is optional. If not set, the server binds to all interfaces (0.0.0.0 / ::0).

  • port must be set before starting the server.

  • Threading, transports, and other advanced options can be customized via Configuration before calling start().

Last updated

Was this helpful?