Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the example usage in README #42

Open
dolftax opened this issue Jan 14, 2016 · 8 comments
Open

Improve the example usage in README #42

dolftax opened this issue Jan 14, 2016 · 8 comments

Comments

@dolftax
Copy link

dolftax commented Jan 14, 2016

How is this supposed to work? Could you please make the Usage section more clear?

@indutny
Copy link
Owner

indutny commented Jun 1, 2016

@dolftax sorry for long delay. Do you have any particular suggestions? What isn't clear in current version?

@TheColorRed
Copy link

TheColorRed commented Jun 7, 2016

For me I can not figure out why socket.io stops working... If I remove the if/else it works, but then adding it back it stops working. Am I doing something wrong? Also you should add a list of options/settings with descriptions to the readme.

Basically, in the end, could you put a basic socket.io connection example in the usage section.

// Import node modules
import http     = require('http');
import socketio = require('socket.io');
import redisio  = require('socket.io-redis');
const sticky    = require('sticky-session');

const server = http.createServer();

const stickyOptions = {
    workers: 1
}

if (!sticky.listen(server, 3000, stickyOptions)) {
    server.once('listening', () => {
        console.log(`Server listening on port 3000`);
    });
} else {
    const io: SocketIO.Server = socketio(server);
    const redis: SocketIORedis.RedisAdapter = redisio({
        host: Config.Redis.Host,
        port: Config.Redis.Port
    });

    // Attach redis to socket.io
    io.adapter(redis);

    io.on('connection', (socket) => {

        console.log('User Connected');

    });
    server.listen(3000);
}

@indutny
Copy link
Owner

indutny commented Jun 16, 2016

I believe you don't need server.listen(3000) below, otherwise I don't see any other problems with this code snippet. Will take a deeper look in a bit.

@indutny
Copy link
Owner

indutny commented Jun 16, 2016

This example appears to be hanging even without sticky-session. How should it be tested?

@TheColorRed
Copy link

TheColorRed commented Jun 16, 2016

I just ran the above code (might need to modify the constants as I was using TypeScript), then attempted to connect from a web browser. Which hit: ws://api.chat.com (Host file modification). It then goes to Nginx to go to the node chat server.

The result should be User Connected is displayed in a console. That never happens and the request just hangs.

Here is the Nginx configuration:

upstream io_nodes {
        ip_hash;
        server 127.0.0.1:3000;
#       server 127.0.0.1:3001;
}

server {
        listen 80;
        server_name api.chat.com;

        location / {
                proxy_pass http://io_nodes/;
                proxy_redirect off;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';

                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-NginX-Proxy true;
                #proxy_cache_bypass $http_upgrade;
        }
}

@indutny
Copy link
Owner

indutny commented Jun 16, 2016

@TheColorRed do you have a reduced test case? May be an .html page that will connect to the websocket server or anything like this?

@TheColorRed
Copy link

Here is basic HTML that works

<doctype html>
<html>
    <head>
        <title>Chat</title>
    </head>
    <body>
        <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
        <script>
            var socket = io('api.chat.com');
        </script>
    </body>
</html>

@indutny
Copy link
Owner

indutny commented Jun 16, 2016

@TheColorRed this appears to be working, could you please verify?

const http     = require('http');
const socketio = require('socket.io');
const redisio  = require('socket.io-redis');
const sticky    = require('sticky-session');

const server = http.createServer((req, res) => {
  if (req.url === '/') {
    res.writeHead(200, {
      'content-type': 'text/html'
    });
    res.end(`<doctype html>
            <html>
              <head>
                <title>Chat</title>
              </head>
              <body>
                <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
                <script>
                  var socket = io('127.0.0.1:3000');
                </script>
              </body>
            </html>`);
    return;
  }
});

const stickyOptions = {
  workers: 1
}

if (!sticky.listen(server, 3000)) {
  server.once('listening', () => {
    console.log(`Server listening on port 3000`);
  });
  return;
}

const io = socketio(server);

io.on('connection', (socket) => {

  console.log('User Connected');

});

// server.listen(3000);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants