Created by Tom Wilson / @twilson63
Tom Wilson
Jack Russell Software
Web and Mobile development for HealthcareIT
President
Github - http://github.com/twilson63
Twitter - @twilson63
Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript.
socket.io
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
<script src="/socket.io/socket.io.js">
</script>
<script>
var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event',
{ my: 'data' });
});
</script>
HTML enhanced for web apps!
<div ng-app>
<h1>{{title}}</h1>
<input type="text" ng-model="title">
</div>
Live Demo
function MainCtrl($scope, $http) {
$http.get('http://jsbin.com/abaqip')
.success(function(data) {
$scope.repos = data;
});
}
<ul>
<li ng-repeat="repo in repos" >
{{name}}
</li>
</ul>
<body ng-app="MyApp">
<ng-view></ng-view>
</body>
angular.module('MyApp', [])
.config(function($routeProvider) {
$routeProvider
.when('/', { template: 'main.html', controller: 'MainCtrl'})
.when('/page2', { template: 'page2.html', controller: 'Page2Ctrl'})
})
Live Demo
<input type="text" ng-model="search">
<ul>
<li
ng-repeat="
repo in repos | filter:search
| orderBy: 'name'" >
{{name}}
</li>
</ul>
Live Demo
<blink>Like its 1999</blink>
app.directive('blink', function() {
return {
template: '<marquee scrollamount="100%">Blink!</marquee>'
}
});
Live Demo
Service
app.factory('socket', function ($rootScope) {
var socket = io.connect();
return {
// insert on
// insert emit
}
});
Service on
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
}
Service emit
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
});
}
Template
<form class="form-search">
<div class="input-append">
<input class="search-query" type="text"
ng-model="query">
<button class="btn" ng-click="search()">
<i class="icon-bolt"></i>
</button>
</div>
</form>
Client Side
$scope.search = function() {
socket.emit('search', $scope.query,
function(res) {
$scope.hogs = res.responseData.results;
});
};
Server Side
var googleUrl = 'https://ajax.googleapis.com/' +
'ajax/services/search/images?v=1.0&';
socket.on('search', function (q, fn) {
request(googleUrl +
'q=HedgeHog+' + q,
{json: true},
function(e,r,b) { fn(b); }
);
});
Template
<li ng-repeat="hog in hogs" >
<a href="#" ng-click="select(hog)">
<div class="media">
<div class="chevron pull-right">
<i class="icon-chevron-right"></i>
</div>
<div class="pull-left">
<img class="media-object"
ng-src="{{hog.tbUrl}}" />
</div>
<div class="media-body">
<h4>
<div ng-bind-html-unsafe="hog.title">
</div>
</h4>
</div>
</div>
</a>
</li>
Client-Side
$scope.select = function(hog) {
$scope.selected = hog.url;
};
Template
<div class="row">
<img ng-src="{{selected}}"></img>
</div>
<div class="row">
<button
style="margin-top: 20px;"
class="btn pull-right"
ng-show="selected"
ng-click="submit()">Submit</button>
</div>
Client Side
$scope.submit = function() {
socket.emit('submit', $scope.selected);
$location.path('/vote');
};
Server Side
socket.on('submit', function(url) {
// add to database
db.put(uuid.v1(), url);
// broadcast to listeners
io.sockets.emit('addHog', url);
});
Client
socket.on('addHog', function(hog) {
$scope.hogs.push(hog);
});
socket.emit('hogs');
Server
socket.on('hogs', function() {
db.createReadStream().on('data', function(data) {
var obj = JSON.parse(data.value);
var hog = {id: data.key, url: obj.url, votes: obj.votes };
socket.emit('addHog', hog);
});
});
Template
<div class="span2" ng-repeat="hog in hogs">
<a href="#" ng-click="vote(hog)">
<img
style="height: 120px; width: 120px;"
ng-src="{{hog.url}}"
/>
</a>
</div>
Client
$scope.vote = function(hog) {
socket.emit('vote', hog);
$location.path('/leaders');
};
Server
socket.on('vote', function(hog) {
db.put(hog.id, JSON.stringify({
url: hog.url,
votes: hog.votes + 1
}));
});
Template
<div class="span2"
ng-repeat="hog in hogs
| orderBy: '-votes'">
<a href="#" ng-click="vote(hog)">
<img
style="height: 120px; width: 120px;"
ng-src="{{hog.url}}"
/>
</a>
<div
style="padding: 30px;font-size: 3em;font-weight: bold;">
{{hog.votes}}
</div>
</div>
Questions