> For the complete documentation index, see [llms.txt](https://www.socketio4j.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.socketio4j.org/server-api/getting-started/server-instance.md).

# 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.

```java
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);

SocketIOServer server = new SocketIOServer(config);
```

{% hint style="warning" %}
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.
{% endhint %}

## Starting the Server

{% tabs %}
{% tab title="Sync" %}

```java
server.start()
```

{% endtab %}

{% tab title="Async" %}

```java
server.startAsync().addListener(future -> {
            if (future.isSuccess()) {
                System.out.println("Server started on " + config.getPort());
            } else {
                System.out.println("Error " + future.cause().getLocalizedMessage());
            }
        });
```

{% endtab %}
{% endtabs %}

## Stopping the Server

Always stop the server gracefully during shutdown.

```java
server.stop();
```

{% hint style="info" %}
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.
{% endhint %}

## Complete Example

{% code title="Server.java" %}

```java
import com.socketio4j.socketio.Configuration;
import com.socketio4j.socketio.SocketIOServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SocketIoServerMain {
    
    private static final Logger log = LoggerFactory.getLogger(SocketIoServerMain.class);
    
    public static void main(String[] args) throws Exception {
        // Create configuration
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        // Create server
        SocketIOServer server = new SocketIOServer(config);
        
        server.addConnectListener(client -> {
            log.info("[/] connected -> " + client.getSessionId());
        });
        server.addDisconnectListener(client -> {
            log.info("[/] disconnected -> " + client.getSessionId());
        });
        
        server.addEventListener("hi", String.class, (client, data, ack) -> {
            //listen to "reply" in client
            log.info("received data : " + data);
            client.sendEvent("reply", "hello");
        });
        // Start server
        server.start();
        log.info("Socket.IO server started on port 9092");

        // Keep JVM alive
        Thread.currentThread().join();
    }
}

```

{% endcode %}

{% hint style="info" %}
Check [Events](https://www.socketio4j.org/events/) for event handling related documentation&#x20;
{% endhint %}

## 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()`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.socketio4j.org/server-api/getting-started/server-instance.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
