{"action":"closed","issue":{"url":"https://api.github.com/repos/btford/angular-socket-io/issues/103","labels_url":"https://api.github.com/repos/btford/angular-socket-io/issues/103/labels{/name}","comments_url":"https://api.github.com/repos/btford/angular-socket-io/issues/103/comments","events_url":"https://api.github.com/repos/btford/angular-socket-io/issues/103/events","html_url":"https://github.com/btford/angular-socket-io/issues/103","id":63000876,"number":103,"title":"Using with MEAN.js, not connecting to server on first load","user":{"login":"karan-kapoor90","id":7921433,"avatar_url":"https://avatars.githubusercontent.com/u/7921433?v=3","gravatar_id":"","url":"https://api.github.com/users/karan-kapoor90","html_url":"https://github.com/karan-kapoor90","followers_url":"https://api.github.com/users/karan-kapoor90/followers","following_url":"https://api.github.com/users/karan-kapoor90/following{/other_user}","gists_url":"https://api.github.com/users/karan-kapoor90/gists{/gist_id}","starred_url":"https://api.github.com/users/karan-kapoor90/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/karan-kapoor90/subscriptions","organizations_url":"https://api.github.com/users/karan-kapoor90/orgs","repos_url":"https://api.github.com/users/karan-kapoor90/repos","events_url":"https://api.github.com/users/karan-kapoor90/events{/privacy}","received_events_url":"https://api.github.com/users/karan-kapoor90/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-19T14:21:16Z","updated_at":"2015-03-27T08:16:35Z","closed_at":"2015-03-27T08:16:35Z","body":"I copied the socket implementation from the 0.4 branch to 0.3 and it works fine. However instead of calling the `this.Socket=io()` in the chatController like it is, in the 0.4 app, I call it in the AuthenticationController, as follows \r\n\r\n    app.controller('AuthenticationController', ['$scope', '$http', '$location', '$state', 'Authentication','Socket','socketFactory',\r\n    function($scope, $http, $location, $state, Authentication,Socket,socketFactory) {\r\n\r\n        $scope.authentication = Authentication;\r\n        if (Authentication.user) \r\n            this.Socket = io();\r\n            //this.Socket = io('http://<some-url>/');\r\n            //this.Socket = socketFactory({ioSocket: io.connect('http://<some-url>/')});\r\n        $scope.userType = ['doctor', 'patient','sponsor'];\r\n        Socket.on('refreshSocket',function(data){\r\n            $scope.authentication.user.socketid = data.myId;\r\n            Authentication.user.socketid = data.myId;\r\n            console.log('####### Refresh from auth cont');\r\n        });\r\n\r\nso it's called right as soon as the user logs in. On the server side, in the socket.io.js\r\n\r\n    io.on('connection', function(socket) {\r\n        //to resolve the location of chat.server.socket.config.js along with other possible files\r\n        console.log('Connection event triggered on server');\r\n        io.to(socket.id).emit('refreshSocket',{myId : socket.id});\r\n        socket.broadcast.emit('isAlive',{user : socket.request.user._id, socketid : socket.id});\r\n        config.files.server.sockets.forEach(function(socketConfiguration) {\r\n            require (path.resolve(socketConfiguration))(io, socket);\r\n        });\r\n    });\r\n\r\nalways prints \r\n\r\n    Connection event triggered on server\r\n\r\nThe server is set to listens to 'chatMessage' events as \r\n\r\n    socket.on('chatMessage', function(message) {\r\n        console.log('%%%%%%%%%%\\n chat event emitted \\n %%%%%%%%%%%%%%%%');\r\n        message.source = socket.request.user._id;\r\n        User.findOne({_id:message.target._id},'socketid', function(err,user){\r\n            if (err){\r\n                message.type = 'status';\r\n                message.text = 'Oops! Something went wrong. Please try again after a while.';\r\n                message.username = '';\r\n                io.to(socket.id).emit('chatMessage', message);\r\n                //do nothing, pass message to be inserted into users list of messages\r\n                return err;\r\n            } else if (user.socketid){\r\n                //checking if the target user has an active connection\r\n                var socketList = io.sockets.server.eio.clients;\r\n                if (socketList[user.socketid] === undefined){\r\n                    //socketid did not get removed correctly, the user is not online\r\n                    saveMessages(socket.request.user._id,message.target._id,message.text);\r\n                    message.type = 'status';\r\n                    message.text = 'Message will be delivered when '+message.target.firstName+' is online!';\r\n                    message.username = '';\r\n                    io.to(socket.id).emit('chatMessage', message);\r\n                    removeSocketId(message.target._id);\r\n                }else {\r\n                    //socketid is legit and is online!\r\n                    message.type = 'message';\r\n                    message.created = Date.now();\r\n                    message.profileImageURL = socket.request.user.profileImageURL;\r\n                    message.username = socket.request.user.firstName;\r\n                    io.to(user.socketid).emit('chatMessage', message);\r\n                    user.socketid='';\r\n                    saveMessages(socket.request.user._id,message.target._id,message.text);\r\n                }\r\n            } else {\r\n                //since target does not have a socketid, save message in chat history and send status message to sending user.\r\n                saveMessages(socket.request.user._id,message.target._id,message.text);\r\n                message.type = 'status';\r\n                message.text = 'Message will be delivered when '+message.target.firstName+' is online!';\r\n                //message.created = Date.now();\r\n                //message.profileImageURL = socket.request.user.profileImageURL;\r\n                message.username = '';\r\n                io.to(socket.id).emit('chatMessage', message);\r\n            }\r\n        });\r\n\r\n    });\r\n\r\n**The problem**\r\n\r\nAs soon as my server starts, I login with a  user and click a button to emit a chatMessage event as follows\r\n\r\n    $scope.sendIm = function(){\r\n            if (this.chatText) {\r\n                var message = {\r\n                    target: $scope.targetUser,\r\n                    text: this.chatText,\r\n                    username: 'Me',\r\n                    profileImageURL: $scope.authentication.user.profileImageURL,\r\n                    created: Date.now()\r\n                };\r\n                //push to end of array, not top\r\n                messages.push(message);\r\n                console.log('******* Sending IM **********');\r\n                Socket.emit('chatMessage',message);\r\n                this.chatText='';   \r\n            }\r\n        };\r\n\r\nI get the `******* Sending IM **********` console.log on the client when I send the message but not the \r\n`%%%%%%%%%%\\n chat event emitted \\n %%%%%%%%%%%%%%%%` on the server console.\r\n\r\nThis means that the server is not getting intimated about the 'chatMessage' event. If I refresh the client page and then click the button to send a message, the chatMessage event is successfully fired and I see the `%%%%%%%%%%\\n chat event emitted \\n %%%%%%%%%%%%%%%%` on the server console.\r\n\r\nAnybody knows what I could be doing wrong?"}}