0

I have a php project up and running that uses websockets,but only in a broadcasting sort of way.Recenly(well...a few hours ago) I started experimenting with node.js and I like it so far.I want to use socket.io's 'rooms' feature in my project.Here is the basic code smippet I have tried so far:(test.js).

var app = require('http').createServer()
, io = require('socket.io').listen(app)
, fs = require('fs');
app.listen(1234);
//var io = require('socket.io').listen(1234);
console.log('IO Server running');
//***Redis client***
var redis = require('redis');
client = redis.createClient();
client2 = redis.createClient();
channel = process.env.CHANNELL  || 'chatroom';
client.on('error', function(err){
      console.log("Error ;" + err);
      });
client.on('message', function(channel, message){
      console.log(message);
      });
client.on('subscribe', function(channel){
                 console.log('Client has subscribed');
      });

//***Event listeners***
//***onopen***
io.on('connection', function(socket){
   socket.emit('message', {'message': 'hello world'});
   console.log('New Connection accepted');
   client.subscribe(channel);
   });
   //***onmessage***

   //***onclose***

I am aware how ridiculously primitive and naive the code above appears,but well,it's been only a few hours... (test.html)--->served with Apache, from a XAMPP project directory

<html>
<head>
 <script type="text/javascript" src="js/jquery.min.js"></script>
 <script type="text/javascript" src="http://localhost:1234/socket.io/socket.io.js"></script>           
 <script type="text/javascript">
 var socket = io.connect();
 </script>
</head>
<title>Primitive websocket in node.js</title>

The server starts dutifully,with no errors,in the console:

info -socket.io started.

however, on the client side there is an error the error:

GET http://localhost/socket.io/1/?t=1392148871226 404 (Not Found) socket.io.js:1659

I have read the available literature online on this particular error but none seem to be helpful.it seems my test.js server must also serve test.html(or something to do with different ports).Otherwise the 404 error.Is it possible,what I am trying to do here? How do I make the error go away? Any suggestion/help would be appreciated.My problem is more or less like this question.

automaton
  • 1,091
  • 1
  • 9
  • 23

1 Answers1

0

Create a directory '/socket.io' in the main directory. Find the 'socket.io.js' file from the the socket.io module installed and put it in the '/socket.io'. Change the related code to:

<script src="socket.io/socket.io.js"></script>

I had similar problem. But after changing to this it works. Right now I have another problem can be seen here: Integrating node.js, socket.io and php web page

Community
  • 1
  • 1
Ahad
  • 37
  • 1
  • 1
  • 7