0

So i am complete beginner to nodeJs and socket io.Some how i have installed both in my server and made one example chat application.

So it works like..if i visit http://domain.com:3000 everything works fine...But is it possible to just uploade index.html anywhere else For example http://domain.com/chat/ and insert code like this below in client file.

 var connection = new WebSocket('ws://domain.com:3000');

My client code

<html>
<head>
    <title>Chat with socket.io and node.js</title>
    <style>
        #chat{
            height:500px;
        }
    </style>
</head>
<body>
    <div id="chat"></div>
    <form id="send-message">
        <input size="35" id="message"></input>
        <input type="submit"></input>
    </form>

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        jQuery(function($){
            var socket = io.connect();
            var $messageForm = $('#send-message');
            var $messageBox = $('#message');
            var $chat = $('#chat');

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit('send message', $messageBox.val());
                $messageBox.val('');
            });

            socket.on('new message', function(data){
                $chat.append(data + "<br/>");
            });
        });
    </script>
</body>
</html>

My server code

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

server.listen(3000);

app.get('/', function(req, res){
    res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket){
    socket.on('send message', function(data){
        io.sockets.emit('new message', data);
    });
});

1 Answer 1

2

You should look into using nginx as a reverse proxy. There are many advantages to having nginx out front like its ability to serve your static files blazingly fast. Its also battle tested, built for high concurrency, and uses the same event loop based approach to I/O. When you want to scale your app, you can easily setup nginx to load balance between a cluster of apps.

If you want to maintain a complete Nodejs stack, you could also take a look at node-http-proxy and use it as a reverse proxy/load balancer as well. However, nodejs is not nearly as efficient with static files due to its need to work through userland for file access. nginx uses sendfile(1) to bypass userland and work directly with the kernel.

tl;dr

You need a reverse proxy

Sign up to request clarification or add additional context in comments.

1 Comment

well thats really confusing !! +1 though for ur explantion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.