Node.js, socket.io: Receiving duplicate requests from every socket.io call

Trying my hand with node.js for the first time, creating a simple real time chat application like everyone else.

Been having an issue with it and last ~2hours can't find a solution/reason for it.

I had this problem initially within my chat application, every single request to the server would be duplicated so messages would appear twice, in the console the joining server would log twice. I've implemented a few solutions as there are a couple of similar issues floating around here but none fixed the problem.

I tried to recreate the issue in a new test app, to see if i could find what I had done wrong.

Server code

var express = require('express');
var routes = require('./routes');
var path = require('path');
var http = require('http');

var app = module.exports = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

app.set('port', 1620);
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}

app.get('/', routes.index);

io.sockets.on('connection', function (socket) {
    socket.on('test', function (data) {
        console.log('Test.' + data);            
    });
});

server.listen(app.get('port'), app.get('ipaddr'), function () {
    console.log('Express server listening on IP/hostname: "' + app.get('ipaddr') + '" and port: "' + app.get('port') + '"');
});

Client code:

'use strict';
angular.module('chatter', []);

angular.module('chatter').controller('chatterCtrl', [
    '$scope', '$http', function ($scope, $http) {
    
    $scope.test = 'Hello';
    
    var socket = io.connect();
    
    $scope.sendTest = function () {
        socket.emit('test', 'ThisIsATest');
    }
}
]);

Where $scope.sendTest is bound to a button click.

Here is the console log...

(Won't let me post picture)

Test.1
Test.1
Test.2
Test.2
Test.3
Test.3
Test.4
Test.4    
Test.5
Test.5
Test.6
Test.6

Unlike other similiar issues this problem is not due;

  • To Favicon being called (have express.favicon, it happens every single call and in all browsers)
  • To event handler being called to many times, the request is always only duplicated, doesn't matter if 4 clients connect, or refresh the request is only ever called twice.

Can anyone see from my code what the issue might be? And if you run this code do you get the same output?

Cheers.

As seen in comments the problem had nothing to do with node.js/socket.io as I intially thought, the issue was that I had declared my angular script twice, causing my angular controller to be called twice for everything.

    <script src="~/Scripts/angular.js" type="text/javascript"></script>
    <script src="~/Scripts/angular.js" type="text/javascript"></script>

Removing the duplicate declaration fixed the issue.