0

I am trying to make a Node.js app that will have an embedded chat. I am using socket.io to create that chat. I have attempted to set up my server / client, but the client does not seem to be connecting. I have my application to set log when sockets connect and disconnect but it never seems to log anything. I figured that the client was being retarded so I opened the Chrome devtools, and typed in:

var socket = io();

oddly, I start seeing a string of failed requests that look like:

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1434334401655-37 404 (Not Found)

So now I'm sure it s my server. here is my server code:

var express = require("express");
var mongoose = require("mongoose");
var io = require("socket.io").listen(app);
// var restAPI = require("./api");

// Set up the application.
var app = express();
app.use(express.static("static"));

// Mount up the rest API
// app.use("/rest", restAPI);

// Default index route.
app.get("/", function(req, res) {
    res.sendFile(__dirname + "/index.html");
});

// This will maintian a transcript of the chat
var transcript = []

// This route will return the transcript data as json
app.get("/transcript", function(req, res) {
    res.json(JSON.stringify(transcript));
});

// Simple socket.io for chat application 
// Lets keep a reference to how many connections we have
var count = 0;
io.sockets.on("connection", function(socket) {
    // Increment the number of clients
    count++
    // Keep a reference to the socket
    var current = socket;
    // Log the connection
    console.log("Connected to: " + current + " at " + Date.now() + ".");
    // Log the current number of users
    console.log("There are now " + count + "users connected.");

    // Handle disconnection
    socket.on("disconnect", function() {
        // Same routine as connect
        console.log(current + ": disconnected at " + Date.now() + ".");
        console.log("There are now " + count + "users connected.");
    });
});

app.listen(3000);

Here is my HTML client:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport">
        <title>Paint</title>
        <!-- <script src="/js/application.js"></script> -->
        <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
    </head>
    <body>
        <header></header>
        <nav></nav>
        <aside>
            <div id="toolbox"></div>
            <!-- display: none; by default -->
            <div id="chat"></div>

            <div class="buttons">
                <div class="toolBoxButton">Toolbox</div>
                <div class="chatButton">Chat</div>
            </div>
        </aside>
        <section></section>
        <footer></footer>
        <script>
            var socket = io();
        </script>
    </body>
</html>

Why can't the client connect?

1 Answer 1

1

I don't know if this is the only issue, but you're calling this:

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

before you assign the app variable so it is undefined.


The request for this URL:

http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1434334401655-37

is how a socket.io connection starts so that is normal. The problem is that the socket.io listener wasn't running properly on the server so nothing was listening for that route.


Also, I don't know if this:

io.sockets.on("connection", function(socket) {...});

works in socket.io v1+. The socket.io doc says to use this:

io.on('connection', function(socket) {...});
Sign up to request clarification or add additional context in comments.

3 Comments

Changing io.sockets.on to io.on didn't change anything. So I tried to change where I connected. I tried to put io.listen(app); right before the connection event handler, but then it tells me I can;t connect socket.io to an express app to connect it to a http.Server instead. So i try io.listen(http.createServer(app));, then I Undefined is not a function on io.on.
@James_Parsons - I can't follow what you tried without seeing the whole sequence of code. Why don't you just follow the instructions here: socket.io/docs? It's all spelled out there. You will need to know what version of socket.io and what version of Express to know which instructions to follow.
@jfiriend00 thanks for that link. I was trying to create an HTTP server in the wring way. Wow Node.js stuff outdated quickly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.