0

I am creating a website that uses php and javascript for most the site. I only want to use NodeJS for the live chat message feature on the site, kind of likes facebook's. Can I use NodeJS without returning any html files? Is that practical?

EDIT: My current server code is this :

var http = require('http');

console.log("before");
var app = http.createServer(function (request, response) {

}).listen(21);

var io = require('socket.io').listen(app);

io.sockets.on('connection', function(socket) {
console.log("Client Connected");
});

Here is relevant client code

<script type="text/javascript">

    var socketio = io.connect("localhost:21");

</script>

So why is it that Client Connected never logs? I have gotten it working a while back when returning html files, but for some reason this isn't working :|.

Wyatt
  • 493
  • 7
  • 18

6 Answers6

1

Sure you can, check out this this, I think it's probably what you are looking for.

curtainrising
  • 249
  • 1
  • 8
1

Yes, but you can't use port 21 (should change other port, I use 3000).

In client html :

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script type="text/javascript">
    var socketio = io.connect("http://localhost:3000");
</script>
hong4rc
  • 3,999
  • 4
  • 21
  • 40
0

Yes you can, but if you want to avoid use NodeJS you can achieve the same target using Websockets for PHP. Look at this library for example Ratchet library for to use Websockets in PHP

Synkronice
  • 179
  • 1
  • 9
0

I think it's a little bit difficult to make live chat in PHP system.
Live chat needs push and pull data from client to the server.
PHP system can pull data(e.g. html) from the server.But I think you usually can't send data to the server dynamically only in PHP. So if you want to make live chat in PHP system, you need to use AJAX to NodeJS API. Please see this post too.
How to integrate nodeJS + Socket.IO and PHP?

EDIT:

I think you should add http or https to io.connect() argument. io.connect('http://localhost:21');

But You should change port because this port is unsafe. For example 3000 would be better.

io.connect('http://localhost:3000');

You should fix port number in server js file.

Community
  • 1
  • 1
ima0123
  • 85
  • 1
  • 10
0

You can use NodeJS for just about anything, and with only a couple hundred lines of Socket.IO you can push and stream data around for numerous scenarios. Lots of info on Github and other sites about this.

David Spenard
  • 789
  • 7
  • 10
0

Think of Node more like a general server, not specifically a web server providing HTML pages. Yes it can be used for HTML, but you don't need to, and I would think it is most frequently used as a RESTful server providing JSON instead.

There are some great socket libraries for Node like rabbit.js which would be perfect for a messaging app. Some even come with some chat examples, such as the more popular socket.io.

Neil Cresswell
  • 1,145
  • 8
  • 20