0

My nodeJS code isn't working for some reason. I'm new to nodeJS, so excuse my ignorance. Here's the server code:

var http = require('http'); console.log("before"); var app = http.createServer(function (request, response) { console.log("Server Created"); }).listen(3000); var io = require('socket.io').listen(app); io.sockets.on('connection', function(socket) { console.log("Client Connected"); }); 

Here is my client code:

<script type="text/javascript"> var socketio = io.connect("http://localhost:3000"); </script> 

I run the server, then go to index.html which contains the client code. I feel like I must be missing something obvious :| Client Connected never prints.

9
  • Any errors in console? Commented Jul 9, 2016 at 18:55
  • No, there aren't errors Commented Jul 9, 2016 at 19:02
  • @Nonemoticoner I was working from a tutorial. Maybe in the client code, io isn't recognized? Not sure where that variable comes from. Commented Jul 9, 2016 at 19:03
  • You are loading the socket.io client-side library in index.html? Commented Jul 9, 2016 at 19:09
  • @gcampbell I would assume so :\ How do I ensure that? Commented Jul 9, 2016 at 19:19

1 Answer 1

1

looks like your server code is just fine.

Have you tried changing localhost to IP Address in your client code like the following:

 ... <script type="text/javascript" src="http://<YOUR_IP>:3000/socket.io/socket.io.js"></script> <script type="text/javascript"> var socket = io('http://<YOUR_IP:3000'); </script> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.