diff --git a/modules/chat/client/controllers/chat.client.controller.js b/modules/chat/client/controllers/chat.client.controller.js index ffb83330..615dfd9e 100644 --- a/modules/chat/client/controllers/chat.client.controller.js +++ b/modules/chat/client/controllers/chat.client.controller.js @@ -6,6 +6,14 @@ angular.module('chat').controller('ChatController', ['$scope', 'Socket', // Create a messages array $scope.messages = []; + // If user is not signed in then redirect back home + if (!Authentication.user) $location.path('/'); + + // Make sure the Socket is connected + if (!Socket.socket) { + Socket.connect(); + } + // Add an event listener to the 'chatMessage' event Socket.on('chatMessage', function(message) { $scope.messages.unshift(message); diff --git a/modules/core/client/services/socket.io.client.service.js b/modules/core/client/services/socket.io.client.service.js index e66224cf..6f47bd68 100644 --- a/modules/core/client/services/socket.io.client.service.js +++ b/modules/core/client/services/socket.io.client.service.js @@ -3,12 +3,15 @@ // Create the Socket.io wrapper service angular.module('core').service('Socket', ['Authentication', '$state', '$timeout', function(Authentication, $state, $timeout) { - // Connect to the Socket.io server only when authenticated - if (Authentication.user) { - this.socket = io(); - } else { - $state.go('home'); - } + + // Connect to Socket.io server + this.connect = function () { + // Connect only when authenticated + if (Authentication.user) { + this.socket = io(); + } + }; + this.connect(); // Wrap the Socket.io 'on' method this.on = function(eventName, callback) {