Chud37
Chud37

Reputation: 5007

Getting the tutorial SocketIO to work

I am trying to get the example from SocketIO working on my domain.

I have managed to get Node.JS installed, along with ExpressJS and SocketIO through the command line.

Now, I am trying to get this to run. On the server side I have:

(located in /nodejs, at the same level as /public_html, not inside it)

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

And then on the client side I have:

(Located in /public_html/socketio.htm)

<!DOCTYPE html>
<head>
    <title>Socket IO Test</title>
    <script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>

    <style type='text/css'>
        * {font-family: 'Trebuchet MS';}
    </style>
</head>
<body>
    <h1>Socket IO Test</h1>
    <script>
    var socket = io.connect('http://localhost/');
        socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
    </script>
</body>
</html>

I don't understand the following questions:

1. Does it matter where my node.js runs from? Should I put it inside /public_html?

2. I couldn't list on port 80, I got the error address in use. It seems to me that that is a common port to use for http traffic, why would SocketIO use that? Or is it because I am running my nodejs outside of a domain? Should I create a subdomain and move my nodejs files (including /node_modules) to a subdomain?

3. What port can I use? I tried 3000, and then changing the http://localhost/ to http://localhost:3000 but that didn't work.

4. I am not running on a local machine, but on my domain. Do I need to change http://localhost/ to reflect that? Aka, http://domain.com ?

Any help would be greatly appreciated!!

Upvotes: 1

Views: 254

Answers (1)

Ravindra Galav
Ravindra Galav

Reputation: 2900

server side ..

var app = require('express')();
var express = require("express")
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(express.static(__dirname + '/nodejs'));


server.listen(8000);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

client side..

<!DOCTYPE html>
<head>
    <title>Socket IO Test</title>
    <script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>

    <style type='text/css'>
        * {font-family: 'Trebuchet MS';}
    </style>
</head>
<body>
    <h1>Socket IO Test</h1>
    <script>
    var socket = io.connect('http://localhost:8000/');
        socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
    </script>
</body>
</html>

and use directory structure as follow..

  • expressocket.js
  • nodejs/

          socketio.htm
    

Upvotes: 1

Related Questions