Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
367 additions
and
29 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Stock Quote Stream powered by socket.io-announce, managed by mixture | ||
|
||
### Setup | ||
Runs on Node.js v0.5.9+. | ||
|
||
* `npm install` | ||
|
||
### Managed mixed cluster | ||
* `node . [n]` - manage all processes, creating a data stream and n socket.io apps. defaults to 4. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var path = require('path') | ||
, connect = require('connect') | ||
, sio = require('socket.io') | ||
, RedisStore = sio.RedisStore | ||
|
||
var port = process.argv[2] || 8880 | ||
, id = process.argv[3] || 0 | ||
, delay = process.argv[4] || 800 | ||
, app = connect.createServer(connect.static(path.join(__dirname, './'))) | ||
, io = sio.listen(app); | ||
|
||
io.configure(function () { | ||
io.set('store', new RedisStore({ nodeId: function () { return id } })) | ||
}) | ||
|
||
io.sockets.on('connection', function (socket) { | ||
socket.emit('nodeId', id); | ||
|
||
socket.on('purchase', function (data, fn) { | ||
data.timestamp = Date.now(); | ||
|
||
setTimeout(function () { // without a delay the transition between purchase and confirm is imperceptible | ||
socket.emit('confirm', data); | ||
socket.broadcast.emit('activity', data); | ||
}, delay) | ||
}); | ||
|
||
socket.on('restart', function (data) { | ||
io.sockets.in('').emit('restart') | ||
}) | ||
}) | ||
|
||
app.listen(port) | ||
|
||
app.on('listening', function () { | ||
console.log('listening on :', app.address()) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Announce doesn't bind a port. | ||
* Announce also doesn't have to worry about where it's sending the data. | ||
* It simply broadcasts to the Redis pub/sub backbone. | ||
*/ | ||
|
||
var announce = require('socket.io-announce').createClient() | ||
|
||
/* fake data stream */ | ||
var symbols = 'THOO GOOF EXIT BOP SDD ALPP RIGM OPPL HPBG'.split(' ') | ||
|
||
function dataStream () { | ||
var n = Math.round(Math.random()*5) | ||
, data = { | ||
id: (Math.abs(Math.random() * Math.random() * Date.now() | 0)) | ||
, symbol: symbols[n] | ||
, price: (Math.random()*1000).toFixed(2) | ||
, n: n | ||
} | ||
announce.emit('quote', data) | ||
} | ||
|
||
dataStream() | ||
setInterval(dataStream, 800) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<style> | ||
body { margin: 0 } | ||
#nodeId { | ||
width: 100%; | ||
height: 100%; | ||
background-color: white; | ||
color: black; | ||
font-size: 12em; | ||
text-align: center; | ||
text-shadow: 4px 4px 1px #e5e5e5; | ||
-webkit-transform: scale(1); | ||
} | ||
#ticker { | ||
position: absolute; | ||
bottom: 0; | ||
width: 100%; | ||
padding: 10px; | ||
overflow: hidden; | ||
white-space: nowrap; | ||
text-align: right; | ||
direction: rtl; | ||
background-color: rgba(0,0,0,.5); | ||
color: white; | ||
text-shadow: 1px 1px 1px #e5e5e5; | ||
height: 1.2em; | ||
} | ||
#ticker span { padding: 10px } | ||
#ticker .reserved { background-color: rgba(255,255,0,.5) } | ||
#ticker .purchased { background-color: rgba(0,255,0,.5) } | ||
#ticker .activity { background-color: rgba(255,0,0,.5) } | ||
</style> | ||
</head> | ||
<body> | ||
<div id=nodeId></div> | ||
<footer id=ticker></footer> | ||
|
||
<script src="/js/jquery-1.6.4.min.js"></script> | ||
<script src="/socket.io/socket.io.js"></script> | ||
<script> | ||
var socket = io.connect() | ||
, nodeId; | ||
|
||
socket.on('nodeId', function (data) { | ||
nodeId = data; | ||
document.title = '[ '+nodeId+' ] - @dshaw demo'; | ||
$('#nodeId').text(nodeId); | ||
}); | ||
|
||
socket.on('quote', function (data) { | ||
$('<span/>', { id: data.id, html: data.symbol + ' : ' + data.price }).appendTo('#ticker'); | ||
|
||
// if the last digit of the prices matches the id, buy. | ||
var m = ((data.price).match(/[-+]?([0-9]*([0-9]))?\.[0-9]+/)) | ||
, watch = m && m[2]; | ||
|
||
if (nodeId && watch == nodeId) { | ||
$('#'+data.id).addClass('reserved'); | ||
data.buyer = nodeId; | ||
data.quantity = Math.round(Math.random()*5)*5; | ||
socket.emit('purchase', data); | ||
} | ||
}); | ||
|
||
socket.on('confirm', function (data) { | ||
$('#'+data.id) | ||
.attr('title','Purchased '+(new Date(data.timestamp))) | ||
.removeClass('reserved') | ||
.addClass('purchased'); | ||
}); | ||
|
||
socket.on('activity', function (data) { | ||
$('#'+data.id) | ||
.attr('title','Purchased '+(new Date(data.timestamp))+' on Node Id '+data.nodeId) | ||
.addClass('activity'); | ||
}); | ||
|
||
socket.on('restart', function () { | ||
location.reload(true); | ||
}); | ||
|
||
$('#nodeId').live('dblclick', function() { socket.emit('restart') }); | ||
</script> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var mix = require('../../').mix() | ||
|
||
var count = process.argv[2] || 4 // || maxes out locally at ~82 | ||
, ioPort = 8880 | ||
, nodeId = 0 | ||
|
||
// announce data server | ||
mix.task('announce').fork('data.js'); | ||
|
||
// socket.io instances | ||
var socketio = mix.task('socket.io', { filename: 'app.js' }) | ||
|
||
for (var i = 0; i < count; i++) { | ||
ioPort++; | ||
nodeId++; | ||
|
||
var worker = socketio.fork({ args: [ioPort, nodeId] }) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "stock-quotes-example" | ||
, "version": "0.1.1" | ||
, "description": "Socket.io-announce Sample App: Stock Quote Stream" | ||
, "author": "Daniel D. Shaw <dshaw@dshaw.com> (http://dshaw.com)" | ||
, "keywords": ["socket.io", "socket.io-announce", "redisstore", "redis"] | ||
, "main": "index" | ||
, "dependencies": { | ||
"connect": ">= 1.7.2" | ||
, "socket.io": ">= 0.8.5" | ||
, "socket.io-announce": ">= 0.1.0" | ||
} | ||
, "engines": { "node": ">= 0.5.9" } | ||
} |
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
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
Oops, something went wrong.