diff --git a/examples/cluster-nginx/README.md b/examples/cluster-nginx/README.md index df46d31763..35c52e6fbf 100644 --- a/examples/cluster-nginx/README.md +++ b/examples/cluster-nginx/README.md @@ -22,6 +22,16 @@ Each node connects to the redis backend, which will enable to broadcast to every $ docker-compose stop server-george ``` +A `client` container is included in the `docker-compose.yml` file, in order to test the routing. + +You can create additional `client` containers with: + +``` +$ docker-compose up -d --scale=client=10 client +# and then +$ docker-compose logs client +``` + ## Features - Multiple users can join a chat room by each entering a unique username diff --git a/examples/cluster-nginx/client/Dockerfile b/examples/cluster-nginx/client/Dockerfile new file mode 100644 index 0000000000..caccfba307 --- /dev/null +++ b/examples/cluster-nginx/client/Dockerfile @@ -0,0 +1,15 @@ +FROM node:14-alpine + +# Create app directory +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +# Install app dependencies +COPY package.json /usr/src/app/ +RUN npm install --prod + +# Bundle app source +COPY . /usr/src/app + +EXPOSE 3000 +CMD [ "npm", "start" ] diff --git a/examples/cluster-nginx/client/index.js b/examples/cluster-nginx/client/index.js new file mode 100644 index 0000000000..e3f6e53ecc --- /dev/null +++ b/examples/cluster-nginx/client/index.js @@ -0,0 +1,13 @@ +const socket = require('socket.io-client')('ws://nginx'); + +socket.on('connect', () => { + console.log('connected'); +}); + +socket.on('my-name-is', (serverName) => { + console.log(`connected to ${serverName}`); +}); + +socket.on('disconnect', (reason) => { + console.log(`disconnected due to ${reason}`); +}); diff --git a/examples/cluster-nginx/client/package.json b/examples/cluster-nginx/client/package.json new file mode 100644 index 0000000000..25ac346a3c --- /dev/null +++ b/examples/cluster-nginx/client/package.json @@ -0,0 +1,15 @@ +{ + "name": "socket.io-chat", + "version": "0.0.0", + "description": "A simple chat client using socket.io", + "main": "index.js", + "author": "Grant Timmerman", + "private": true, + "license": "MIT", + "dependencies": { + "socket.io-client": "^2.4.0" + }, + "scripts": { + "start": "node index.js" + } +} diff --git a/examples/cluster-nginx/docker-compose.yml b/examples/cluster-nginx/docker-compose.yml index 3e18c90a66..abfe474308 100644 --- a/examples/cluster-nginx/docker-compose.yml +++ b/examples/cluster-nginx/docker-compose.yml @@ -45,6 +45,11 @@ server-ringo: environment: - NAME=Ringo +client: + build: ./client + links: + - nginx + redis: image: redis:alpine expose: diff --git a/examples/cluster-nginx/nginx/nginx.conf b/examples/cluster-nginx/nginx/nginx.conf index cc9d5870c7..46554b25aa 100644 --- a/examples/cluster-nginx/nginx/nginx.conf +++ b/examples/cluster-nginx/nginx/nginx.conf @@ -24,8 +24,12 @@ http { } upstream nodes { - # enable sticky session - ip_hash; + # enable sticky session with either "hash" (uses the complete IP address) + hash $remote_addr consistent; + # or "ip_hash" (uses the first three octets of the client IPv4 address, or the entire IPv6 address) + # ip_hash; + # or "sticky" (needs commercial subscription) + # sticky cookie srv_id expires=1h domain=.example.com path=/; server server-john:3000; server server-paul:3000; diff --git a/examples/cluster-nginx/server/Dockerfile b/examples/cluster-nginx/server/Dockerfile index d1f9075727..caccfba307 100644 --- a/examples/cluster-nginx/server/Dockerfile +++ b/examples/cluster-nginx/server/Dockerfile @@ -1,4 +1,4 @@ -FROM mhart/alpine-node:6 +FROM node:14-alpine # Create app directory RUN mkdir -p /usr/src/app @@ -6,7 +6,7 @@ WORKDIR /usr/src/app # Install app dependencies COPY package.json /usr/src/app/ -RUN npm install +RUN npm install --prod # Bundle app source COPY . /usr/src/app