On Github oroce / nodejsbp-socket.io
by Robert Oroszi (oroce)
var http = require('http'), express = require('express'), fs = require('fs'), app = express(); app.use(express.static(__dirname + '/static')); app.get('/', function(req, res) { res.send('nodeWHAT!?'); }); http.createServer(app).listen(3000, function (err) { if (err) return console.log(err); });
var http = require('http'), express = require('express'), fs = require('fs'), app = express(), sio = require('socket.io'); app.use(express.static(__dirname + '/static')); app.get('/', function(req, res) { res.send('nodeWHAT!?'); }); var server = http.createServer(app); var io = sio.listen(server); server.listen(3000, function (err) { if (err) return console.log(err); });
@@ -1,7 +1,8 @@ var http = require('http'), express = require('express'), fs = require('fs'), - app = express(); + app = express(), + sio = require('socket.io'); app.use(express.static(__dirname + '/static')); @@ -9,6 +10,9 @@ app.get('/', function(req, res) { res.send('nodeWHAT!?'); }); -http.createServer(app).listen(3000, function (err) { +var server = http.createServer(app); +var io = sio.listen(server); + +server.listen(3000, function (err) { if (err) return console.log(err); });
Server should response to http://localhost:3000/socket.io
read more about npm startServer:wiki connection*, message, disconnect
Client:wiki connect, reconnect, message, disconnect, etc
io.on('connection', function (socket){ // `socket` is a connected client });
socket.on('message',function (data){ // client sent something awesome for us });
io.on('connection', function (socket){ socket.on('message', function (data){ io.sockets.send(data); }); });
@@ -14,6 +14,12 @@ var server = http.createServer(app); var io = sio.listen(server); +io.on('connection', function (socket){ + socket.on('message', function (data){ + io.sockets.send(data); + }); +}); + server.listen(3000, function (err) { if (err) return console.log(err); });
<!DOCTYPE html> <html> <body> + <h1>socket.io chat example for node.js budapest</h1> + <h3 id="status"></h3> + <ul id="messages"></ul> + + <input type="text" /> + <button>send</button> <script src="/socket.io/socket.io.js"></script> <script src="/client.js"></script> </body>
@@ -1 +1,32 @@ -var socket = io.connect(); \ No newline at end of file +var socket = io.connect(), + messagesEl = document.querySelector('#messages'), + statusEl = document.querySelector('#status'), + textEl = document.querySelector('input'), + sendBtnEl = document.querySelector('button'), + me = prompt('tell me your name sunshine, you\'d be grateful'); + +[ 'connect', 'reconnect', 'connecting', 'disconnect' ].forEach(function( evName ){ + socket.on( evName, function(){ + statusEl.textContent = evName; + }); +}); + +socket.on('message', addMessage); + +sendBtnEl.addEventListener('click', function(){ + var text = ( textEl.value||'' ).trim(); + var date = new Date(); + socket.send(JSON.stringify({ + from: me, + text: text, + date: date.getHours() + ':' + date.getMinutes() + })); + textEl.value = ''; +}, false); + +function addMessage(data){ + var message = JSON.parse(data); + var li = document.createElement('li'); + li.innerHTML = message.from + ':' + message.text + ' (at ' + message.date + ')'; + messagesEl.appendChild( li ); +}
this is the v8 heap.
that amount of memory which can be allocated for our app by v8
you have to restart your application
or you can use a process manager (forever, monit, mon, supervisord) which RESTARTS for you
DUMMY EXPLANATION: garbage collector is responsible to automatically clean the memory, it tries to find out what parts of memory are garbage and removes them from memory
Has anybody done that?
DEMO SITE
redis is a key-value store
not such string-string representation, it supports different data-structures, too
..and also it has pubsub feature
FYI: a client (name it foobar) connected to me, I stored everything in redis and this is currently know about it:
{ "id": "foobar", "name": "awesome minion" }
I'll let you know if something happen between us
ps: nodejs cluster works on the same machine
these are useful for a http application but not a realtime app