Skip to content

Instantly share code, notes, and snippets.

@theRemix
Created April 1, 2014 23:30
Show Gist options
  • Select an option

  • Save theRemix/9925161 to your computer and use it in GitHub Desktop.

Select an option

Save theRemix/9925161 to your computer and use it in GitHub Desktop.
Socket.io Angular Express Mongo
/**
* Module dependencies
*/
var express = require('express'),
routes = require('./routes'),
tweets = require('./routes/tweets'),
api = require('./routes/api'),
http = require('http'),
path = require('path');
// var app = module.exports = express();
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(3000);
/**
* Configuration
*/
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
// development only
if (app.get('env') === 'development') {
app.use(express.errorHandler());
}
// production only
if (app.get('env') === 'production') {
// TODO
}
/**
* Routes
*/
// serve index and view partials
app.get('/', routes.index);
app.get('/tweets', tweets.list);
app.post('/tweets', tweets.add);
// app.get('/partials/:name', routes.partials);
// JSON API
// app.get('/api/name', api.name);
// redirect all others to the index (HTML5 history)
// app.get('*', routes.index);
io.sockets.on('connection', function (socket) {
socket.on('submit', function(data){
// console.log(data);
tweets.getList(function(tweets){
// socket.emit('update', tweets);
socket.broadcast.emit('update', tweets);
});
});
});
/**
* Start Server
*/
// http.createServer(app).listen(app.get('port'), function () {
// console.log('Express server listening on port ' + app.get('port'));
// });
'use strict';
/* Controllers */
angular.module('myApp.controllers', []).
controller('AppCtrl', function ($scope, $http, socket) {
socket.on('update', function(data){
console.log(data);
updateTweets(data);
});
$scope.tweets = $scope.$root.tweets = [];
$scope.$root.$watchCollection(
"tweets",
function( newValue, oldValue ) {
// console.log(newValue);
// console.log(oldValue);
}
);
$http.get('/tweets').
success(function (data, status, headers, config) {
// $scope.tweets = data;
updateTweets(data);
}).
error(function (data, status, headers, config) {
$scope.tweets = [
{
body : data, // this will be an error
timestamp : Date.now()
}
];
});
$scope.createTweet = function(tweet){
$http.post('/tweets', tweet).
success(function (data, status, headers, config) {
socket.emit('submit', { data : data });
$scope.tweets.unshift(data);
// clear the text here
angular.element(
document.querySelector('#tweet_body')
).val("");
}).
error(function (data, status, headers, config) {
$scope.tweets = [
{
body : data, // this will be an error
timestamp : Date.now()
}
];
});
};
// this uses $watchCollection
// don't overwrite $scope.tweets
function updateTweets (data) {
// WATCHING AINT WORKING
/*for (var i = 0; i < data.length; i++) {
$scope.tweets.push(data[i]);
};*/
$scope.tweets = data;
}
});
var mongoose = require('mongoose');
mongoose.connect('mongodb://167.216.15.200/TweetPress');
var TweetSchema = mongoose.Schema({
body : String,
timestamp : {
type : Date,
default : Date.now()
}
});
var Tweet = mongoose.model('Tweet', TweetSchema);
exports.list = function (req,res) {
_getList(function(tweets){
res.json(tweets);
})
};
// xhr POST /tweets
exports.add = function (req,res) {
var newTweet = new Tweet(req.body);
newTweet.save(function(err,tweet){
if(err) res.json(err);
res.json(tweet);
});
}
exports.getList = _getList;
function _getList(cb) {
Tweet.find().sort('-timestamp').exec(function(err,tweets){
if(err) res.json(err);
cb(tweets)
});
}
extends layout
block body
div(ng-controller='AppCtrl')
h2 TweetPress
p Press your tweets like a pro!
//- form input
textarea#tweet_body( ng-model="tweet.body", ng-maxlength='140', required, ng-required='string' )
br
button( ng-click="createTweet(tweet)") Press It
//- list all tweets
ul#tweets
li(ng-repeat="tweet in tweets")
p.body {{ tweet.body }}
span.timestamp
{{ tweet.timestamp | date : "mediumDate" }}
{{ tweet.timestamp | date : "shortTime" }}
script(src='js/lib/angular.min.js')
script(src='js/lib/socket.io.min.js')
script(src='js/app.js')
script(src='js/services.js')
script(src='js/controllers.js')
script(src='js/filters.js')
script(src='js/directives.js')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment