名前空間
名前空間は、単一の共有接続(「多重化」とも呼ばれます)を介してアプリケーションのロジックを分割できる通信チャネルです。


はじめに
各名前空間には、独自の
io.of("/orders").on("connection", (socket) => {
socket.on("order:list", () => {});
socket.on("order:create", () => {});
});
io.of("/users").on("connection", (socket) => {
socket.on("user:list", () => {});
});
const orderNamespace = io.of("/orders");
orderNamespace.on("connection", (socket) => {
socket.join("room1");
orderNamespace.to("room1").emit("hello");
});
const userNamespace = io.of("/users");
userNamespace.on("connection", (socket) => {
socket.join("room1"); // distinct from the room in the "orders" namespace
userNamespace.to("room1").emit("holà");
});
const orderNamespace = io.of("/orders");
orderNamespace.use((socket, next) => {
// ensure the socket has access to the "orders" namespace, and then
next();
});
const userNamespace = io.of("/users");
userNamespace.use((socket, next) => {
// ensure the socket has access to the "users" namespace, and then
next();
});
が備わっています。
使用例
- 承認されたユーザーのみがアクセスできる特別な名前空間を作成して、それらのユーザーに関連するロジックをアプリケーションの他の部分から分離する場合
const adminNamespace = io.of("/admin");
adminNamespace.use((socket, next) => {
// ensure the user has sufficient rights
next();
});
adminNamespace.on("connection", socket => {
socket.on("delete user", () => {
// ...
});
});
- アプリケーションに複数のテナントがあるため、テナントごとに1つの名前空間を動的に作成する場合
const workspaces = io.of(/^\/\w+$/);
workspaces.on("connection", socket => {
const workspace = socket.nsp;
workspace.emit("hello");
});
メイン名前空間
これまで、`\` と呼ばれるメイン名前空間と対話していました。 `io` インスタンスは、そのすべてのメソッドを継承します。
io.on("connection", (socket) => {});
io.use((socket, next) => { next() });
io.emit("hello");
// are actually equivalent to
io.of("/").on("connection", (socket) => {});
io.of("/").use((socket, next) => { next() });
io.of("/").emit("hello");
一部のチュートリアルでは `io.sockets` についても言及している場合がありますが、これは単に `io.of("/")` のエイリアスです。
io.sockets === io.of("/")
カスタム名前空間
カスタム名前空間を設定するには、サーバー側で `of` 関数を呼び出すことができます。
const nsp = io.of("/my-namespace");
nsp.on("connection", socket => {
console.log("someone connected");
});
nsp.emit("hi", "everyone!");
クライアントの初期化
同一オリジンバージョン
const socket = io(); // or io("/"), the main namespace
const orderSocket = io("/orders"); // the "orders" namespace
const userSocket = io("/users"); // the "users" namespace
クロスオリジン/Node.jsバージョン
const socket = io("https://example.com"); // or io("https://example.com/"), the main namespace
const orderSocket = io("https://example.com/orders"); // the "orders" namespace
const userSocket = io("https://example.com/users"); // the "users" namespace
上記の例では、WebSocket 接続は1つだけ確立され、パケットは自動的に正しい名前空間にルーティングされます。
以下の場合、多重化は無効になります。
- 同じ名前空間の複数作成
const socket1 = io();
const socket2 = io(); // no multiplexing, two distinct WebSocket connections
- 異なるドメイン
const socket1 = io("https://first.example.com");
const socket2 = io("https://second.example.com"); // no multiplexing, two distinct WebSocket connections
- forceNew オプションの使用
const socket1 = io();
const socket2 = io("/admin", { forceNew: true }); // no multiplexing, two distinct WebSocket connections
動的な名前空間
正規表現を使用して、動的に名前空間を作成することもできます。
io.of(/^\/dynamic-\d+$/);
または関数を使用して
io.of((name, auth, next) => {
next(null, true); // or false, when the creation is denied
});
`connection` イベントで新しい名前空間にアクセスできます。
io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
const namespace = socket.nsp;
});
`of()` メソッドの戻り値は、親名前空間と呼ばれるもので、そこから
- ミドルウェアを登録
const parentNamespace = io.of(/^\/dynamic-\d+$/);
parentNamespace.use((socket, next) => { next() });
できます。ミドルウェアは、各子名前空間に自動的に登録されます。
- イベントをブロードキャストする
const parentNamespace = io.of(/^\/dynamic-\d+$/);
parentNamespace.emit("hello"); // will be sent to users in /dynamic-1, /dynamic-2, ...
注意
既存の名前空間は、動的な名前空間よりも優先されます。例:
// register "dynamic-101" namespace
io.of("/dynamic-101");
io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
// will not be called for a connection on the "dynamic-101" namespace
});
完全な API
Namespace インスタンスによって公開される完全な API は、こちらにあります。