Skip to content

Instantly share code, notes, and snippets.

@patrickbrandt
Last active August 30, 2023 21:28
Show Gist options
  • Save patrickbrandt/1cd98a02c42e9e22a5a9 to your computer and use it in GitHub Desktop.
Save patrickbrandt/1cd98a02c42e9e22a5a9 to your computer and use it in GitHub Desktop.

Revisions

  1. patrickbrandt revised this gist Dec 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ app.use('/', routes);

    server.listen('3000');
    ```
    You can now emit socket.io events from any route handler (./routes/index in this example):
    You can now emit socket.io events from any route handler (```./routes/index``` in this example):
    ```javascript
    var express = require('express');
    var router = express.Router();
  2. patrickbrandt revised this gist Dec 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ app.use('/', routes);

    server.listen('3000');
    ```
    You can now emit socket.io events from any route handler:
    You can now emit socket.io events from any route handler (./routes/index in this example):
    ```javascript
    var express = require('express');
    var router = express.Router();
  3. patrickbrandt revised this gist Nov 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Socket.io integration between and Express and Angular app is a breeze. I'll outline the Express implementation first, then outline Angular integration.
    Socket.io integration between an Express and Angular app is a breeze. I'll outline the Express implementation first, then outline Angular integration.

    # Express + socket.io
    ## Reference socket.io in layout.jade
  4. patrickbrandt revised this gist Nov 9, 2015. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -76,19 +76,19 @@ Create a service that wraps the socket.io global object:
    angular.module('example')
    .factory('io', ['$window', '$rootScope', function($window, $rootScope) {
    var _socket = $window.io('//localhost:3000');
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    ```
    Use the io service in a controller:
    ```javascript
  5. patrickbrandt revised this gist Nov 9, 2015. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -77,18 +77,18 @@ angular.module('example')
    .factory('io', ['$window', '$rootScope', function($window, $rootScope) {
    var _socket = $window.io('//localhost:3000');
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    ```
    Use the io service in a controller:
    ```javascript
  6. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -77,18 +77,18 @@ angular.module('example')
    .factory('io', ['$window', '$rootScope', function($window, $rootScope) {
    var _socket = $window.io('//localhost:3000');
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    ```
    Use the io service in a controller:
    ```javascript
  7. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -80,7 +80,7 @@ angular.module('example')
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the client view
    //NOTE: calling $apply will ensure that any model changes are reflected in the view
    $rootScope.$apply();
    });
    },
  8. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ Include the following code in your app.js module (other standard Express module
    var express = require('express');
    var http = require('http');
    var io = require('socket.io');
    var routes = require('../routes/index');
    var routes = require('./routes/index');

    var app = express();

  9. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ Include the following code in your app.js module (other standard Express module
    var express = require('express');
    var http = require('http');
    var io = require('socket.io');
    var routes = require('../routes/index');

    var app = express();

    @@ -52,6 +53,8 @@ io.on('connection', function(socket) {
    console.log('socket.io connection made');
    });

    app.use('/', routes);

    server.listen('3000');
    ```
    You can now emit socket.io events from any route handler:
  10. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -56,11 +56,16 @@ server.listen('3000');
    ```
    You can now emit socket.io events from any route handler:
    ```javascript
    var express = require('express');
    var router = express.Router();

    router.post('/', function(req, res, next) {
    req.io.emit('some_event');
    //do some stuff
    req.io.emit("some_other_event"); //we did some stuff - emit a related event
    });

    module.exports = router;
    ```
    #Angular + socket.io
    Create a service that wraps the socket.io global object:
  11. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,8 @@ html(ng-app="example")
    script(type='text/javascript' src='/angular-resource/angular-resource.js')
    script(type='text/javascript' src='/angular-route/angular-route.js')
    script(type='text/javascript' src='/javascripts/app.js')
    script(type='text/javascript' src='/javascripts/controllers.js')
    script(type='text/javascript' src='/javascripts/services.js')
    script(type='text/javascript' src='/javascripts/controllers.js')
    ```
    ## Create socket.io middleware
    Include the following code in your app.js module (other standard Express module dependancies and middleware left out for brevity):
  12. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ Socket.io integration between and Express and Angular app is a breeze. I'll outl

    # Express + socket.io
    ## Reference socket.io in layout.jade
    ```npm install socket.io --save``` and then reference in your layout.jade file:
    ```npm install socket.io --save``` and then reference socket.io in your layout.jade file:
    ```jade
    doctype html
    html(ng-app="example")
  13. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ Socket.io integration between and Express and Angular app is a breeze. I'll outl

    # Express + socket.io
    ## Reference socket.io in layout.jade
    ```npm install socket.io``` and then reference in your layout.jade file:
    ```npm install socket.io --save``` and then reference in your layout.jade file:
    ```jade
    doctype html
    html(ng-app="example")
  14. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -82,7 +82,7 @@ angular.module('example')
    };
    }]);
    ```
    Use the service in a controller:
    Use the io service in a controller:
    ```javascript
    angular.module('example')
    .controller('MainCtrl', ['io', function(io) {
  15. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -67,20 +67,20 @@ Create a service that wraps the socket.io global object:
    ```javascript
    angular.module('example')
    .factory('io', ['$window', '$rootScope', function($window, $rootScope) {
    var _socket = $window.io('//localhost:3000');
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the client view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    var _socket = $window.io('//localhost:3000');
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    //NOTE: calling $apply will ensure that any model changes are reflected in the client view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    ```
    Use the service in a controller:
    ```javascript
  16. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -66,8 +66,8 @@ router.post('/', function(req, res, next) {
    Create a service that wraps the socket.io global object:
    ```javascript
    angular.module('example')
    .factory('io', ['$window', '$rootScope', function($window, $rootScope) {
    var _socket = $window.io('//localhost:3000');
    .factory('io', ['$window', '$rootScope', function($window, $rootScope) {
    var _socket = $window.io('//localhost:3000');
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
  17. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,8 @@ angular.module('example')
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    $rootScope.$apply(); //NOTE: calling $apply will ensure that any model changes are reflected in the client view
    //NOTE: calling $apply will ensure that any model changes are reflected in the client view
    $rootScope.$apply();
    });
    },
    emit: function(eventType, data) {
  18. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,7 @@ angular.module('example')
    _socket.on(eventType, function() {
    cb();
    $rootScope.$apply(); //NOTE: calling $apply will ensure that any model changes are reflected in the client view
    });
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
  19. patrickbrandt revised this gist Nov 6, 2015. No changes.
  20. patrickbrandt revised this gist Nov 6, 2015. No changes.
  21. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -57,9 +57,9 @@ server.listen('3000');
    You can now emit socket.io events from any route handler:
    ```javascript
    router.post('/', function(req, res, next) {
    req.io.emit('some event');
    req.io.emit('some_event');
    //do some stuff
    req.io.emit("some other event - now that we've done stuff");
    req.io.emit("some_other_event"); //we did some stuff - emit a related event
    });
    ```
    #Angular + socket.io
    @@ -80,4 +80,14 @@ angular.module('example')
    }
    };
    }]);
    ```
    Use the service in a controller:
    ```javascript
    angular.module('example')
    .controller('MainCtrl', ['io', function(io) {
    this.state = 'something';
    io.on('some_event', function() {
    this.state = 'something else';
    }.bind(this));
    }]);
    ```
  22. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,7 @@ router.post('/', function(req, res, next) {
    req.io.emit("some other event - now that we've done stuff");
    });
    ```
    #Angluar + socket.io
    #Angular + socket.io
    Create a service that wraps the socket.io global object:
    ```javascript
    angular.module('example')
  23. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Socket.io integration between and Express and Angular app is a breeze. I'll outl
    ```npm install socket.io``` and then reference in your layout.jade file:
    ```jade
    doctype html
    html(ng-app="angular_example")
    html(ng-app="example")
    head
    title= title
    link(rel='stylesheet', href='/bootstrap/dist/css/bootstrap.css')
    @@ -61,4 +61,23 @@ router.post('/', function(req, res, next) {
    //do some stuff
    req.io.emit("some other event - now that we've done stuff");
    });
    ```
    #Angluar + socket.io
    Create a service that wraps the socket.io global object:
    ```javascript
    angular.module('example')
    .factory('io', ['$window', '$rootScope', function($window, $rootScope) {
    var _socket = $window.io('//localhost:3000');
    return {
    on: function(eventType, cb) {
    _socket.on(eventType, function() {
    cb();
    $rootScope.$apply(); //NOTE: calling $apply will ensure that any model changes are reflected in the client view
    });
    },
    emit: function(eventType, data) {
    _socket.emit(eventType, data);
    }
    };
    }]);
    ```
  24. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,6 @@ You can now emit socket.io events from any route handler:
    router.post('/', function(req, res, next) {
    req.io.emit('some event');
    //do some stuff
    req.io.emit('some other event - now that we\'ve done stuff');
    req.io.emit("some other event - now that we've done stuff");
    });
    ```
  25. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -58,5 +58,7 @@ You can now emit socket.io events from any route handler:
    ```javascript
    router.post('/', function(req, res, next) {
    req.io.emit('some event');
    //do some stuff
    req.io.emit('some other event - now that we\'ve done stuff');
    });
    ```
  26. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -53,4 +53,10 @@ io.on('connection', function(socket) {
    });

    server.listen('3000');
    ```
    You can now emit socket.io events from any route handler:
    ```javascript
    router.post('/', function(req, res, next) {
    req.io.emit('some event');
    });
    ```
  27. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,8 @@ app.use(function(req, res, next) {
    next();
    });
    io.on('connection', function(socket) {
    log.info('socket.io connection made');
    //log.info('socket.io connection made');
    console.log('socket.io connection made');
    });

    server.listen('3000');
  28. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -50,4 +50,6 @@ app.use(function(req, res, next) {
    io.on('connection', function(socket) {
    log.info('socket.io connection made');
    });

    server.listen('3000');
    ```
  29. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ html(ng-app="angular_example")
    script(type='text/javascript' src='/javascripts/services.js')
    ```
    ## Create socket.io middleware
    Include the following code in your app.js module (other standard Express module dependancies left out for brevity):
    Include the following code in your app.js module (other standard Express module dependancies and middleware left out for brevity):
    ```javascript
    var express = require('express');
    var http = require('http');
  30. patrickbrandt revised this gist Nov 6, 2015. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions socket.io_Express_Angular.md
    Original file line number Diff line number Diff line change
    @@ -21,4 +21,33 @@ html(ng-app="angular_example")
    script(type='text/javascript' src='/javascripts/app.js')
    script(type='text/javascript' src='/javascripts/controllers.js')
    script(type='text/javascript' src='/javascripts/services.js')
    ```
    ## Create socket.io middleware
    Include the following code in your app.js module (other standard Express module dependancies left out for brevity):
    ```javascript
    var express = require('express');
    var http = require('http');
    var io = require('socket.io');

    var app = express();

    /**
    * Create HTTP server.
    */

    var server = http.createServer(app);

    /**
    * Setup custom app middleware
    */

    /* setup socket.io */
    io = io(server);
    app.use(function(req, res, next) {
    req.io = io;
    next();
    });
    io.on('connection', function(socket) {
    log.info('socket.io connection made');
    });
    ```