Skip to content

Commit

Permalink
Add http cluster example
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 11, 2017
1 parent b754cff commit 58ea1f8
Show file tree
Hide file tree
Showing 11 changed files with 1,257 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/cluster-httpd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# Socket.IO Chat with httpd & redis

A simple chat demo for socket.io

## How to use

Install [Docker Compose](https://docs.docker.com/compose/install/), then:

```
$ docker-compose up -d
```

And then point your browser to `http://localhost:3000`.

This will start four Socket.IO nodes, behind a httpd proxy which will loadbalance the requests (using a cookie for sticky sessions, see [cookie](http://httpd.apache.org/docs/2.4/fr/mod/mod_proxy_balancer.html)).

Each node connects to the redis backend, which will enable to broadcast to every client, no matter which node it is currently connected to.

```
# you can kill a given node, the client should reconnect to another node
$ docker-compose stop server-george
```

## Features

- Multiple users can join a chat room by each entering a unique username
on website load.
- Users can type chat messages to the chat room.
- A notification is sent to all users when a user joins or leaves
the chatroom.
51 changes: 51 additions & 0 deletions examples/cluster-httpd/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

httpd:
build: ./httpd
links:
- server-john
- server-paul
- server-george
- server-ringo
ports:
- "3000:80"

server-john:
build: ./server
links:
- redis
expose:
- "3000"
environment:
- NAME=John

server-paul:
build: ./server
links:
- redis
expose:
- "3000"
environment:
- NAME=Paul

server-george:
build: ./server
links:
- redis
expose:
- "3000"
environment:
- NAME=George

server-ringo:
build: ./server
links:
- redis
expose:
- "3000"
environment:
- NAME=Ringo

redis:
image: redis:alpine
expose:
- "6379"
2 changes: 2 additions & 0 deletions examples/cluster-httpd/httpd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM httpd:2.4-alpine
COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf
52 changes: 52 additions & 0 deletions examples/cluster-httpd/httpd/httpd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

Listen 80

ServerName localhost

LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so

LoadModule headers_module modules/mod_headers.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule unixd_module modules/mod_unixd.so

User daemon
Group daemon

ErrorLog /proc/self/fd/2

Header add Set-Cookie "SERVERID=sticky.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

<Proxy "balancer://nodes_polling">
BalancerMember "http://server-john:3000" route=john
BalancerMember "http://server-paul:3000" route=paul
BalancerMember "http://server-george:3000" route=george
BalancerMember "http://server-ringo:3000" route=ringo
ProxySet stickysession=SERVERID
</Proxy>

<Proxy "balancer://nodes_ws">
BalancerMember "ws://server-john:3000" route=john
BalancerMember "ws://server-paul:3000" route=paul
BalancerMember "ws://server-george:3000" route=george
BalancerMember "ws://server-ringo:3000" route=ringo
ProxySet stickysession=SERVERID
</Proxy>

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) balancer://nodes_ws/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) balancer://nodes_polling/$1 [P,L]

ProxyTimeout 3
Loading

0 comments on commit 58ea1f8

Please sign in to comment.