简体   繁体   中英

Use Socket.io and Node.js on existing page (with nginx)?

I'm trying to expand an existing Webpage (NGINX + PHP5 + MySQL) with a real time API that feeds some additional data (for example, an in-page chat).

After going trough some standalone testscripts, etc, I'm actually already having troubles just to set up a simple chat example: Socket.io doesnt want to work, apparently because of dependencies.

I'm trying to use NodeJS/Socket.io only as alternative to fdajax - I never intended to build my whole (already existing) page on nodejs. So, my main question is, how can I 'implement' those two libraries, and still make them work (even if socket.io.js isnt delivered by nodejs )

My current error is ReferenceError: require is not defined and ReferenceError: io is not defined when including socket.io.js . Apparently, the reason is that the function require() probably doesnt work properly, since - well, nodejs isnt delivering the whole page, but PHP does (so, I guess, the whole require() stuff is missing).

Does someone know how I can get socket.io to work, whitout having nodejs deliver the whole page, but instead still using the already existing NGINX+PHP setup?

I tried a couple of things, but couldnt find a good hint yet. The target basically is, to just include socket.io.js (and other required stuff), connect to the nodejs webserver over the specified port (8080, or whatever), and then write some code.

Thanks for the help.

Here is a full fledged example from another answer/question https://stackoverflow.com/a/14709236/1489655


You can setup a proxy for the websocket traffic.

tcp {
    upstream websockets {
        ## node processes
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
        server 127.0.0.1:8004; 

        check interval=3000 rise=2 fall=5 timeout=1000;
    }   

    server {
        listen 127.0.0.1:80;
        server_name _;

        tcp_nodelay on;
        proxy_pass websockets;
    }
}

Source: http://www.letseehere.com/reverse-proxy-web-sockets

This should allow you to connect to socket.io via

var socket = new io.Socket('ws://localhost');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM