Skip to content

cmgchess/socketio-redis-pubsub-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Prerequisites

Additional helpful resources

Additional Tools

Here is a MSI installer I found for Redis.

Setup

  1. Clone repository
  2. cd into server and npm install

Nginx conf

http {

    upstream backend {
        hash '$remote_addr $cookie_zzz $http_user_agent';
	server 127.0.0.1:1212;
        server 127.0.0.1:1213;
    }
		
    server {
        listen 80;
	root E:\\socket\\server;

	location / {
            proxy_pass http://backend;
        }
    }
}

events { }

https://stackoverflow.com/questions/59124543/nginx-not-loadbalancing-in-case-of-ip-hash

Running client

  1. cd intoclient
  2. run http-server . --cors --port=5500

Running server

  1. cd into server and open 2 terminals for the same directory
  2. Set 2 different ports ( ex: export PORT=1212 in one terminal and export PORT=1213 in the other)
  3. Start nginx with the given configuration
  4. npm start the server on both terminals

Now Server 1 will be started on PORT=1212 and Server 2 on PORT=1213 and the Client on PORT=5500. Two servers will be load balanced using nginx on PORT=80 so the client will be directed to either Server 1 or 2 when it hits Port 80. Hash based load balancing is enabled (to achieve stickiness) since nginx open source doesn't allow cookie based load balancing. Go to http://127.0.0.1:5500/ with 2 browsers (Edge and Chrome for example) then the 2 instances will be connected to Server 1 and 2 respectively.
Redis will act as a PubSub mechanism and is enabled using Socket.IO Redis Adapter.
NOTE: Redis adapter will not store anything, it will only do PubSub.
If you want you can store data in Redis as well.

Architecture

User logged in Edge Browser User logged in Chrome Browser