Namespace

Namespaces

Namespaces provide logical separation of features and event handling within the SocketIO server. They allow different parts of an application to operate independently using a single physical connection.

Namespaces do not create separate socket connections. A client connects once and can join multiple namespaces over the same underlying WebSocket/TCP session.


Default Namespace

If a client connects without specifying a namespace, it is attached to the default namespace ("").

Configuration config = new Configuration();
config.setPort(9092);

SocketIOServer server = new SocketIOServer(config);
// default namespace exists implicitly
server.start();

Custom Namespace

Custom namespaces separate application concerns and event scopes.

SocketIOServer server = new SocketIOServer(config);

Namespace chat = server.addNamespace("/chat");
Namespace admin = server.addNamespace("/auth");

server.start();

Please do not forget to add "/", its always "/namespace" NOT just "namespace".

Each namespace defines:

  • its own event listeners

  • its own connection lifecycle

  • its own authorization logic

  • its own broadcast operations


Public Namespace Example (/chat)

The /chat namespace is open and allows general messaging without authentication.

Server

Client


Authenticated Namespace Example (/auth)

The /auth namespace restricts access using authorization logic executed during connection.

Server

Authorized Client

Unauthorized Client

Expected server output:


Last updated

Was this helpful?