provide a better user experience as a result • We also remove the need to make redundant polling requests back to the server. • We use WebSockets for sending/receiving JSON
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
chat demo (-m for minimal install) ✓ Javascript example code (-c if you prefer CoffeeScript) ✓ Plain HTML for views (-j if you prefer Jade) Next, run the following commands: ! cd my_app [sudo] npm install ! To start your app: ! node app.js
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
files and libraries • CSS stores CSS files • STATIC stores public files like images, font files, and other assets • TEMPLATES stores HTML templates for the single page app to render on the client • VIEWS stores HTML files that are rendered from the server for the initial page
must always exist ! // Make 'ss' available to all modules and the browser console window.ss = require('socketstream'); ! ss.server.on('disconnect', function(){ console.log('Connection down :-('); }); ! ss.server.on('reconnect', function(){ console.log('Connection back up :-)'); }); ! ss.server.on('ready', function(){ ! // Wait for the DOM to finish loading jQuery(function(){ // Load app require('/app'); ! }); ! });
must always exist ! // Make 'ss' available to all modules and the browser console window.ss = require('socketstream'); ! ss.server.on('disconnect', function(){ console.log('Connection down :-('); }); ! ss.server.on('reconnect', function(){ console.log('Connection back up :-)'); }); ! ss.server.on('ready', function(){ ! // Wait for the DOM to finish loading jQuery(function(){ // Load app require('/app'); ! }); ! });
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
for GZipping assets • cookieParser - for handling user tracking • favicon - for serving a favicon.ico file • session - for handling sessions • static - for serving static assets
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
ss.publish.all('newMessage', message); // Broadcast the message to everyone Server // Listen out for newMessage events coming from the server ss.event.on('newMessage', function(message) { // do something with the message }); Client
file after calling req.use('session') middleware ! req.session.channel.subscribe('disney') ! req.session.channel.unsubscribe('kids') ! req.session.channel.reset() // unsubscribes the session from every channel ! req.session.channel.list() // shows what channels are subscribed to Server (subscribe/unsubscribe the session )
file ss.publish.channel('disney', 'chatMessage', {from: 'jerry', message: 'Has anyone seen Tom?'}); Server (publish to channel) // in a /client/code file ss.event.on('chatMessage', function(msg, channelName){ console.log('The following message was sent to the ' + channelName + ' channel:', msg); }); Client (receive channel message)
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility
production use Client-side code organisation Building PubSub APIs HTML / CSS / JS code preprocessing Session Management Building custom APIs on top of WS HTML Templates Web Workers Connect middleware compatibility