On Github ruhmesmeile / socketstream-presentation
- Jonas Ulrich / @tsnmp
Further information at the Mozilla Developer Network or the official RFCs.
It provides the structure and basic building blocks you need to create rich social/chat apps, multiplayer games, trading platforms, sales dashboards, or any other kind of web app that needs to display realtime streaming data.
Twitter: @socketstream Google Group: http://groups.google.com/group/socketstream IRC channel: #socketstream on freenode Created by Owen Barnes (GitHub)
Optional modules (officially maintained and supported):ss-sockjs, ss-console, ss-coffee, ss-jade, ss-stylus, ss-less, ss-hogan, ss-coffeekup
socketstream new <name_of_your_project>
cd <name_of_your_project> [sudo] npm install
node app.js
http://localhost:3000
ss.session.store.use('redis', config.db);
ss.publish.transport.use('redis');
ss.client.templateEngine.use(require('ss-hogan'));
ss.ws.transport.use(require('ss-sockjs'));
ss.http.middleware.prepend(ss.http.connect.bodyParser());
// Define a single-page client called 'main' ss.client.define('main', { view: 'app.html', css: ['app.css'], code: ['libs/jquery.min.js', 'app', 'system'], tmpl: '*' });
// Serve main client on the root URL ss.http.route('/', function (req, res) { res.serveClient('main'); });
// Start web server var server = https.createServer(options, ss.http.middleware); server.listen(443); // Start SocketStream ss.start(server);
// Make 'ss' available to all modules and the browser console window.ss = require('socketstream');
// Add handling for disconnect-events ss.server.on('disconnect', function () { console.log('connection is down'); });
// Add handling for reconnect-events ss.server.on('reconnect', function () { console.log('connection is back up'); });
// Initialize app when server signals ready ss.server.on('ready', function () { // Wait for DOM to load jQuery(function () { require('/app').init(); }); };
// Called from entry.js, single initialization point exports.init = function () { // Get current board from server ss.rpc('board.current', initializeBoard); };
// Actions accessible through client/rpc (e.g. 'board.current' // where current is the action & board the controller) exports.actions = function(req, res, socketstream) { return { current: function () { res(null, calculatedResult); }, // Calls can take arbitrary number of arguments get: function (boardId) { ... } } }
var notifyUsers = function (user, callback) { // Publish a message to all connected clients ss.publish.all('newUser', user); };
ss.event.on('newUser', function (user) { // Handle the new user });
// Register for channel boardUpdates req.session.channel.subscribe('boardUpdates') // Publish a message to that channel ss.publish.channel('boardUpdates', 'update', user); // Handle that message on the client ss.event.on('update', function(user, channelName){ ... });
// Add database connection to the SS.api ss.api.add('db', client); // Add enviroment const to the SS.api ss.api.add('enviroment', env);
// Access the database ss.db.get('boards:'+boardId+':positions:'+position, function (err, userId) { ... } ); // Or access the environment if (ss.environment === "production") { ... }
Sessions can be shared between the initial HTTP-request and the WebSocket-connection. For every RPC-call that should load session-data the following has to be added:
// Add ss-session middleware, so we have access to the // shared session (especially session.facebook, // set during initial http-request) exports.actions = function(req, res, socketstream) { req.use('session'); return { ... } }
ss.http.route('/', function (req, res) { res.setHeader('P3P', 'CP="IDC DSP ..."'); res.serveClient('main'); });
Any questions?
Slides were written using reveal.js