├── .gitignore ├── .npmignore ├── Gruntfile.coffee ├── LICENSE.txt ├── README.md ├── bower.json ├── build ├── angular-faye.js ├── angular-faye.min.js ├── server.js └── test.js ├── package.json ├── src └── angular-faye.coffee └── test ├── index.html └── test.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /node_modules 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | **/.* 2 | bower.json 3 | /node_modules 4 | /bower_components 5 | /test 6 | -------------------------------------------------------------------------------- /Gruntfile.coffee: -------------------------------------------------------------------------------- 1 | module.exports = (grunt) -> 2 | grunt.loadNpmTasks('grunt-contrib-coffee') 3 | grunt.loadNpmTasks('grunt-contrib-uglify') 4 | grunt.loadNpmTasks('grunt-contrib-watch') 5 | grunt.loadNpmTasks('grunt-release') 6 | 7 | grunt.config.init 8 | pkg: grunt.file.readJSON "package.json" 9 | coffee: 10 | default: 11 | files: 12 | "build/angular-faye.js": "src/angular-faye.coffee" 13 | "build/test.js": "test/test.coffee" 14 | uglify: 15 | options: 16 | banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n' 17 | build: 18 | src: 'build/<%= pkg.name %>.js' 19 | dest: 'build/<%= pkg.name %>.min.js' 20 | release: 21 | options: 22 | npm: false 23 | watch: 24 | scripts: 25 | files: ['src/*', 'test/*'] 26 | tasks: ['default'] 27 | 28 | grunt.registerTask "default", ["coffee", "uglify"] 29 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2016 Monterail.com LLC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular.js + Faye 2 | 3 | Faye client wrapper for angular 4 | 5 | ## Example 6 | 7 | ### CoffeeScript 8 | 9 | ```coffee 10 | app = angular.module('myapp', ['faye']) 11 | 12 | app.factory 'Faye', ['$faye', ($faye) -> 13 | $faye("http://localhost:9292/faye") # set faye url in one place 14 | ] 15 | 16 | @TestCtrl = ($scope, $http, Faye) -> 17 | # Publish 18 | Faye.publish("/channel-1", {msg: "hello"}) 19 | 20 | # Subscribe 21 | $scope.data = [] 22 | Faye.subscribe "/channel-2", (msg) -> 23 | $scope.data.push msg 24 | 25 | # Get just once (using $q - promise) 26 | $scope.data = Faye.get("/channel-3") 27 | ``` 28 | 29 | 30 | ### Configure Faye client 31 | 32 | ```coffee 33 | app.factory 'Faye', ['$faye', ($faye) -> 34 | $faye "http://localhost:9292/faye", {retry: 5}, (client) -> 35 | client.disable("websocket") 36 | ] 37 | ``` 38 | 39 | ## Development 40 | 41 | ```bash 42 | npm install 43 | grunt watch 44 | ``` 45 | 46 | ## Testing 47 | 48 | ``` 49 | node ./build/server.js 50 | ``` 51 | 1. Modify test/test.coffee. 52 | 2. Open test/index.html. 53 | 3. You should see executed events in console. 54 | 55 | ## Contributing 56 | 57 | 1. Fork it 58 | 2. Create your feature branch (`git checkout -b my-new-feature`) 59 | 3. Commit your changes (`git commit -am 'Add some feature'`) 60 | 4. Push to the branch (`git push origin my-new-feature`) 61 | 5. Create new Pull Request 62 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-faye", 3 | "version": "0.2.2", 4 | "homepage": "https://github.com/monterail/angular-faye", 5 | "description": "Angular.js + Faye", 6 | "main": "build/angular-faye.js", 7 | "keywords": [ 8 | "angular", 9 | "faye", 10 | "ruby" 11 | ], 12 | "authors": [ 13 | "Tymon Tobolski " 14 | ], 15 | "license": "MIT", 16 | "ignore": [ 17 | "**/.*", 18 | "node_modules", 19 | "bower_components", 20 | "app/bower_components", 21 | "test", 22 | "tests" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /build/angular-faye.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | angular.module("faye", []); 3 | 4 | angular.module("faye").factory("$faye", [ 5 | "$q", "$rootScope", function($q, $rootScope) { 6 | return function(url, modifiers, fun) { 7 | var client, scope; 8 | scope = $rootScope; 9 | client = new Faye.Client(url, modifiers); 10 | if (typeof fun === "function") { 11 | fun(client); 12 | } 13 | return { 14 | client: client, 15 | publish: function(channel, data) { 16 | return this.client.publish(channel, data); 17 | }, 18 | subscribe: function(channel, callback) { 19 | return this.client.subscribe(channel, function(data) { 20 | return scope.$apply(function() { 21 | return callback(data); 22 | }); 23 | }); 24 | }, 25 | get: function(channel) { 26 | var deferred, sub; 27 | deferred = $q.defer(); 28 | sub = this.client.subscribe(channel, function(data) { 29 | scope.$apply(function() { 30 | return deferred.resolve(data); 31 | }); 32 | return sub.cancel(); 33 | }); 34 | return deferred.promise; 35 | } 36 | }; 37 | }; 38 | } 39 | ]); 40 | 41 | }).call(this); 42 | -------------------------------------------------------------------------------- /build/angular-faye.min.js: -------------------------------------------------------------------------------- 1 | /*! angular-faye 2015-12-01 */ 2 | (function(){angular.module("faye",[]),angular.module("faye").factory("$faye",["$q","$rootScope",function(a,b){return function(c,d,e){var f,g;return g=b,f=new Faye.Client(c,d),"function"==typeof e&&e(f),{client:f,publish:function(a,b){return this.client.publish(a,b)},subscribe:function(a,b){return this.client.subscribe(a,function(a){return g.$apply(function(){return b(a)})})},get:function(b){var c,d;return c=a.defer(),d=this.client.subscribe(b,function(a){return g.$apply(function(){return c.resolve(a)}),d.cancel()}),c.promise}}}}])}).call(this); -------------------------------------------------------------------------------- /build/server.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'), 2 | path = require('path'), 3 | http = require('http'), 4 | deflate = require('permessage-deflate'), 5 | faye = require('../node_modules/faye/node/faye-node'); 6 | 7 | var SHARED_DIR = __dirname + '/..', 8 | PUBLIC_DIR = SHARED_DIR + '/public', 9 | 10 | bayeux = new faye.NodeAdapter({mount: '/bayeux', timeout: 20}), 11 | port = '8000' 12 | 13 | bayeux.addWebsocketExtension(deflate); 14 | 15 | var server = http.createServer(); 16 | 17 | bayeux.attach(server); 18 | server.listen(Number(port)); 19 | 20 | bayeux.on('publish', function(clientId, channel, data){ 21 | console.log('[PUBLISH] ' + clientId + ' # ' + channel + ' -> ' + data.msg); 22 | }); 23 | 24 | bayeux.on('subscribe', function(clientId, channel) { 25 | console.log('[SUBSCRIBE] ' + clientId + ' -> ' + channel); 26 | }); 27 | 28 | bayeux.on('unsubscribe', function(clientId, channel) { 29 | console.log('[UNSUBSCRIBE] ' + clientId + ' -> ' + channel); 30 | }); 31 | 32 | bayeux.on('disconnect', function(clientId) { 33 | console.log('[DISCONNECT] ' + clientId); 34 | }); 35 | 36 | console.log('Listening on ' + port); -------------------------------------------------------------------------------- /build/test.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | var app; 3 | 4 | app = angular.module('myapp', ['faye']); 5 | 6 | app.factory('Faye', [ 7 | '$faye', function($faye) { 8 | var options; 9 | options = { 10 | endpoints: { 11 | websocket: 'http://ws.example.com/' 12 | }, 13 | retry: 5 14 | }; 15 | return $faye("http://localhost:8000/bayeux", options, function(client) { 16 | return client.disable('autodisconnect'); 17 | }); 18 | } 19 | ]); 20 | 21 | app.controller("TestCtrl", function($scope, $http, Faye) { 22 | Faye.publish("/channel-1", { 23 | msg: "hello" 24 | }); 25 | $scope.data = []; 26 | Faye.subscribe("/channel-2", function(msg) { 27 | return $scope.data.push(msg); 28 | }); 29 | return $scope.data = Faye.get("/channel-3"); 30 | }); 31 | 32 | }).call(this); 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-faye", 3 | "version": "0.2.2", 4 | "authors": [ 5 | "Tymon Tobolski ", 6 | "Monterail" 7 | ], 8 | "repository": "git://github.com/monterail/angular-faye.git", 9 | "description": "Angular.js + Faye", 10 | "main": "build/angular-faye.js", 11 | "dependencies": { 12 | "angular": ">= 1.0.0", 13 | "faye": ">= 1.1.1", 14 | "permessage-deflate": "" 15 | }, 16 | "devDependencies": { 17 | "grunt": "~0.4.1", 18 | "grunt-contrib-coffee": "~0.7.0", 19 | "grunt-contrib-uglify": "~0.2.2", 20 | "grunt-contrib-watch": "~0.5.2", 21 | "grunt-release": "~0.5.1" 22 | }, 23 | "keywords": [ 24 | "angular", 25 | "faye", 26 | "ruby" 27 | ], 28 | "license": "MIT", 29 | "homepage": "http://github.com/monterail/angular-faye", 30 | "ignore": [ 31 | "**/.*", 32 | "node_modules", 33 | "bower_components", 34 | "test", 35 | "tests" 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /src/angular-faye.coffee: -------------------------------------------------------------------------------- 1 | angular.module "faye", [] 2 | 3 | angular.module("faye").factory "$faye", ["$q", "$rootScope", ($q, $rootScope) -> 4 | (url, modifiers, fun) -> 5 | scope = $rootScope 6 | client = new Faye.Client(url, modifiers) 7 | fun?(client) 8 | 9 | 10 | client: client 11 | publish: (channel, data) -> 12 | @client.publish channel, data 13 | 14 | subscribe: (channel, callback) -> 15 | @client.subscribe channel, (data) -> 16 | scope.$apply -> 17 | callback(data) 18 | 19 | get: (channel) -> 20 | deferred = $q.defer() 21 | sub = @client.subscribe(channel, (data) -> 22 | scope.$apply -> 23 | deferred.resolve data 24 | sub.cancel() 25 | ) 26 | deferred.promise 27 | ] 28 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /test/test.coffee: -------------------------------------------------------------------------------- 1 | app = angular.module('myapp', ['faye']) 2 | 3 | app.factory 'Faye', ['$faye', ($faye) -> 4 | 5 | options = { 6 | endpoints: { 7 | websocket: 'http://ws.example.com/' 8 | }, 9 | retry: 5 10 | } 11 | 12 | $faye "http://localhost:8000/bayeux", options, (client) -> 13 | client.disable('autodisconnect'); 14 | ] 15 | 16 | app.controller "TestCtrl", ($scope, $http, Faye) -> 17 | # Publish 18 | Faye.publish("/channel-1", {msg: "hello"}) 19 | 20 | # Subscribe 21 | $scope.data = [] 22 | Faye.subscribe "/channel-2", (msg) -> 23 | $scope.data.push msg 24 | 25 | # Get just once (using $q - promise) 26 | $scope.data = Faye.get("/channel-3") 27 | 28 | --------------------------------------------------------------------------------