Created
July 6, 2014 20:46
-
-
Save piyuesh23/b577a00d884177e15527 to your computer and use it in GitHub Desktop.
Getting sockets to work with /linnovate/mean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Package.json ### | |
"socket.io": "0.9.16" | |
### Bower.json ### | |
"socket.io-client": "0.9.16" | |
npm install | |
bower install | |
### config/assests.json ### | |
Add the following to js object | |
"bower_components/socket.io-client/dist/socket.io.js" | |
### Angular Service for handling sockets ### | |
'use strict'; | |
angular.module('mean.posts') | |
.factory('socket', function($rootScope) { | |
// Make sure the port number here is different from the one that application is running on | |
// Connecting socket on same port won't allow broadcasting | |
var socket = io.connect('', {port: 3004}); | |
return { | |
on: function (eventName, callback) { | |
socket.on(eventName, function () { | |
var args = arguments; | |
$rootScope.$apply(function () { | |
callback.apply(socket, args); | |
}); | |
}); | |
}, | |
emit: function (eventName, data, callback) { | |
socket.emit(eventName, data, function () { | |
var args = arguments; | |
$rootScope.$apply(function () { | |
if (callback) { | |
callback.apply(socket, args); | |
} | |
}); | |
}); | |
} | |
}; | |
}); | |
### Creating http server and running it on the port mentioned above to handle socket connections ### | |
### Modify server.js like following ### | |
'use strict'; | |
// Requires meanio | |
var mean = require('meanio'); | |
// Creates and serves mean application | |
mean.serve({ /*options placeholder*/ }, function(app, config) { | |
console.log('Mean app started on port ' + config.port + ' (' + process.env.NODE_ENV + ')'); | |
var server = require('http').createServer(app).listen(3004); | |
var io = require('socket.io').listen(server); | |
/** | |
* List of events that we will be emitting | |
* | |
* - notification | |
*/ | |
io.sockets.on('connection', function (socket) { | |
socket.on('notification', function(notice){ | |
socket.broadcast.emit('notification', {data: notice}); | |
}); | |
// Add other events here. | |
}); | |
}); | |
### Example of emitting a socket from angular end ### | |
### package/articles/public/controller/articles.js ### | |
'use strict'; | |
angular.module('mean.articles').controller('ArticlesController', | |
['$scope', '$stateParams', '$location', 'Global', 'Articles', 'socket', | |
function($scope, $stateParams, $location, Global, Articles, socket) { | |
// Catch the brodcasted data here on all the connected clients | |
socket.on('notification', function(response) { | |
console.log(response); | |
}); | |
$scope.create = function(isValid) { | |
if (isValid) { | |
var article = new Articles({ | |
title: this.title, | |
content: this.content | |
}); | |
article.$save(function(response) { | |
// Emit the response and catch it on server to broadcast it. | |
socket.emit('notification', response); | |
$location.path('articles/' + response._id); | |
}); | |
this.title = ''; | |
this.content = ''; | |
} else { | |
$scope.submitted = true; | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm curious and concerned about listening on an entirely different port. How does this differ from the normal listening on the same port as the app? For example, I recently modified the latest MEAN code by doing the following in the meanio package:
<!... Inside serve() declaration >
var app = require('./bootstrap')(options, database);
var server = http.createServer(app);
var io = socket.listen(server);
// Start the app by listening on , optional hostname
server.listen(config.port, config.hostname);
<!... Later >
callback(app, config, io);
And emit events from within server.js, upon callback. Now, I'm fairly new to using sockets...and I'd like to know whether my solution differs from yours and which of our solutions is more effective?
Thanks for sharing in advance as well for your time spent discussing this :)