> 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/server-api-4.0-fr/prise-en-main/server-instance.md).

# Instance du serveur

## Créer un serveur

Pour démarrer un serveur Socket.IO, créez un `Configuration`, définissez l'adresse de liaison et le port, puis démarrez le serveur.

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

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

{% hint style="warning" %}
Lors de l'utilisation de frameworks tels que **Spring Boot**, assurez-vous d'importer `com.socketio4j.socketio.Configuration` et non des classes du framework portant le même nom (par exemple, `org.springframework.context.annotation.Configuration`), afin d'éviter les conflits d'importation.
{% endhint %}

## Démarrer le serveur

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

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

{% endtab %}

{% tab title="Asynchrone" %}

```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 %}

## Arrêter le serveur

Arrêtez toujours le serveur proprement lors de l'arrêt.

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

{% hint style="info" %}
socketio4j ajoute un hook d'arrêt après le démarrage du serveur ; il arrête le serveur proprement dans certains scénarios, pas tous. Il est toujours recommandé d'arrêter explicitement le serveur.
{% endhint %}

## Exemple complet

{% 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 {
        // Créer la configuration
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        // Créer le serveur
        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) -> {
            //écouter "reply" côté client
            log.info("received data : " + data);
            client.sendEvent("reply", "hello");
        });
        // Démarrer le serveur
        server.start();
        log.info("Socket.IO server started on port 9092");

        // Maintenir la JVM en vie
        Thread.currentThread().join();
    }
}

```

{% endcode %}

{% hint style="info" %}
Vérifier [Événements](https://www.socketio4j.org/events/events-fr/) pour la documentation relative à la gestion des événements&#x20;
{% endhint %}

## Remarques

* `hostname` est optionnel. S'il n'est pas défini, le serveur se lie à toutes les interfaces (`0.0.0.0` / `::0`).
* `port` **doit** être défini avant de démarrer le serveur.
* Le threading, les transports et d'autres options avancées peuvent être personnalisés via `Configuration` avant d'appeler `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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.socketio4j.org/server-api/server-api-4.0-fr/prise-en-main/server-instance.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
