I am new in this socket. I using backend node js and frontend angularjs.
My data printing in service but unable to get that data into controller.
server.js:
var server = app.listen(port);
var io = require('socket.io').listen(server);
io.on('connection', function(socket) {
setInterval(function() {
socket.emit('addCustomer', {
time : new Date() + "8989"
});
}, 1000);
});
service.js:
var app = angular.module('myApp');
app.factory('socket', ['$rootScope',
function($rootScope) {
var socket = io.connect('http://localhost:9090');
return {
// replace with the code below
on : function() {//on: function(addCustomer){
socket.on("addCustomer", function(customer) {
console.log("Socket Factory - customer added", customer);
return customer;
});
},
emit : function(addCustomer, data) {
console.log("rk");
console.log(data);
socket.emit(addCustomer, data);
}
};
}]);
controller.js:
var app = angular.module('myApp');
app.controller('CustomerCtrl', function($scope, socket) {
socket.on('addCustomer', function(data) {
console.log(data);
console.log("--")
$scope.time = data.time;
});
})
Output printing in console in factory file. but I unable to pass that data into controller. pls anyone help to resolve this.
output in console:
Socket Factory - customer added Object {time: "Thu Sep 29 2016 16:52:44 GMT+0530 (IST)8989"}
socketService.js:11 Socket Factory - customer added Object {time: "Thu Sep 29 2016 16:52:45 GMT+0530 (IST)8989"}
socketService.js:11 Socket Factory - customer added Object {time: "Thu Sep 29 2016 16:52:46 GMT+0530 (IST)8989"}