Node.js + Socket.io + Apache

后端 未结 2 818
攒了一身酷
攒了一身酷 2020-12-24 04:04

I\'m looking for a way to integrate Node.js + Socket.io + Apache in the following way: I want apache to continue serving HTML / JS files. I want node.js to listen for connec

相关标签:
2条回答
  • 2020-12-24 04:34

    Serve your static content from Apache port 80 and serve your dynamic/data content over a Socket.IO server on port 8080. You don't need the app = require('http').createServer(handler) in your Socket.IO app

    Apache port 80 |-------------| clients |------------| Socket.IO port 8080

    var io = require('socket.io').listen(8080);
    
    io.sockets.on('connection', function (socket) {
      io.sockets.emit('this', { will: 'be received by everyone'});
    
      socket.on('clientMSG', function (from, msg) {
        console.log('I received a private message by ', from, ' saying ', msg);
      });
    
      socket.on('disconnect', function () {
        sockets.emit('user disconnected');
      });
    });
    
    0 讨论(0)
  • 2020-12-24 04:43

    AWS + APACHE + NODEJS + SOCKET.IO + ANGULARJS

    SERVER SIDE
    This worked for me on a production server running apache on port 80 and NodeJS on port 8000. Change the NodeJS port by your desired option…

    1. Create a folder named “nodejs” for the files of the NodeJS server at /var/www/html
    2. Run Nodejs on a different port than 80, for example port 8000
    3. Execute command: a2enmod proxy_http
    4. Execute command: a2enmod proxy_wstunnel
    5. Put the next 2 lines at the end of the following file: /etc/apache2/apache2.conf

      LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
      LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so 
      
    6. Put the next 12 lines at the end of the following file: /sites-available/000-default.conf
      (If you have a different site created by you, put the lines there)

      RewriteEngine On
      RewriteCond %{REQUEST_URI}  ^socket.io          [NC]
      RewriteCond %{QUERY_STRING} transport=websocket [NC]
      RewriteRule /{.*}       ws://localhost:8000/$1  [P,L]
      
      RewriteCond %{HTTP:Connection} Upgrade [NC]
      RewriteRule /(.*) ws://localhost:8000/$1 [P,L]
      
      ProxyPass /nodejs http://localhost:8000/
      ProxyPassReverse /nodejs http://localhost:8000/
      
      
      ProxyPass /socket.io http://localhost:8000/socket.io
      ProxyPassReverse /socket.io http://loacalhost:8000/socket.io
      
      ProxyPass /socket.io ws://localhost:8000/socket.io
      ProxyPassReverse /socket.io ws://localhost:8000/socket.io
      
    7. sudo service apache2 restart


    CLIENT SIDE
    I use the following library to implement Socket.io in AngularJS, but I think this guide
    is useful too for a basic Javascript implementation of socket.io technology.

    To call my PHP server: example.com
    To call NodeJS server: example.com/nodejs
    To call NodeJS Socket: example.com <---this call will be done by default by the library

    I hope help you!

    0 讨论(0)
提交回复
热议问题