Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript How to utilize Nodejs for a small module on an existing PHP site

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #205185
    fooman
    Participant

    I have a site I’ve created through the use of PHP. It has a bunch of pages, htaccess rewrites/permalinks, SCSS usage, etc etc. Fairly ‘basic’ folder structure (ie. not using MVC).

    Let’s say I want to create a small module in the header that displays how many users are currently on the site. Just a little box that says “2 users”, for example. Something simple that is an add-on for the site.

    How do I utilize Node to do this? Every tutorial I come across pretty much starts from scratch and creates an app on it’s own, or utilizes Express and an MVC structure. I’m aware of how to create an app that does this standalone locally, but tying the functionality to an existing site is where I’m lost.

    I’m not asking how to do the actual functionality… but how to tie it in with a full-on existing site that has nothing to do with Node as it currently exists.

    #205188
    Alen
    Participant

    @fooman Why do you feel you need NodeJS for this use case? How will you determine user sessions between PHP/Node? Could PHP sessions be of any use, then you can just cunt how many active sessions you have.

    $number_of_users = count(scandir(ini_get("session.save_path"))) - 2;
    

    Source.

    If you want to go with Node you’ll have to set up a Reverse Proxy for your node application. So that your server can respond accordingly and forward any requests to the “app”.

    Reverse Proxy with nginx

    http {
      upstream node_app {
        server 127.0.0.1:3000;
      }
      server {
       listen *:80;
       root /path/to/node/application/public;
        location / {
         try_files $uri $uri/index.html @node;
        }
        location @node {
         proxy_pass http://node_app;
        }
      }
    }
    

    Your Node App, ex app.js

    var http = require('http');
    http.createServer(function (request, response) {
     time = (new Date).getTime().toString();
     response.writeHeader(200, {"Content-Type": "text/plain"});
     response.end(time);
    }).listen(3000, "127.0.0.1");
    console.log("Server running at http://127.0.0.1:3000/");
    

    Then from the CLI you need to run node app.js for example. But in production you’ll most likely want something like pm2 so you can supervise node process.

    Hope that gets you started.
    Best,
    Alen

    #205212
    fooman
    Participant

    PHP is usable, but once it’s set it’s there until page refresh.

    The reason I want to go with Node is because I’m trying to learn how to utilize it on an existing site, and a simple “user counter” that updates as people enter/leave the site seems like a simple way to figure stuff out haha

    I was able to get a pretty simple app going on Media Temple that counted users and instantly updated a little counter on the site. Nothing crazy. But I’m not sure if I’m doing it arse-backwards.

    For example, in my client-side js I have the following:
    socket = io.connect("server's ip:3000");

    I’m not sure if this is the best way to do it, but 90% of the tutorials I see use localhost, which doesn’t work on a hosting server.

    And to be frank, the port thing confuses me. 3000 works… on this server. If I try to use 8000 it doesn’t. I get the ‘address in use’ error.

    In my server-side node code I have the following:
    var port = process.env.PORT || 3000;
    Is that correct? I dunno. Does it work… yes.

    Ports confuse me :(

    #205214
    fooman
    Participant

    BTW thank you for your reply!! :)
    I’m a n00b when it comes to nginx so I’ll probably need to learn me something.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.