├── .bowerrc ├── LICENSE ├── README.md ├── bower.json └── ngSocket.js /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "lib", 3 | "json": "bower.json" 4 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Christopher EnyTC 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 | # ngSocket [![BOWER version](https://badge-me.herokuapp.com/api/bower/chrisenytc/ng-socket.png)](http://badges.enytc.com/for/bower/chrisenytc/ng-socket) 2 | 3 | Angular Module for Socket.io 4 | 5 | ## Requirements 6 | 7 | - AngularJS 1.3.0+ 8 | - Socket.IO 1.2.0+ 9 | 10 | ## Installing 11 | 12 | Simply download the `ngSocket.js` file and add it to your web application. Just make sure it's included after the AngularJS script. 13 | 14 | ## Usage 15 | 16 | 1. Add the `ngSocket` module as a dependency in your AngularJS app; 17 | 2. Inject the `$socket` factory wherever you need to use Socket.IO; 18 | 3. You're done! 19 | 20 | ## Example 21 | ```html 22 | 23 | 24 | 25 | 39 | ``` 40 | ## Cancelling a subscription automatically on scope destruction 41 | 42 | If you want to unsubscribe from an event automatically on scope destruction, just pass the current scope to `on` method: 43 | 44 | ```javascript 45 | $socket.on('someEvent', $scope, function(data) { 46 | ... 47 | }); 48 | ``` 49 | 50 | ## Changing which URL to connect to 51 | 52 | By default, socket.io will connect to the same server that delivered the HTML page that the code is running on. 53 | If you want to connect to a different server, you can provide a different URL in the config event of your AngularJS 54 | module: 55 | 56 | ````javascript 57 | angular 58 | .module("MyModule", ['ngSocket']) 59 | .config(["$socketProvider", function ($socketProvider) { 60 | $socketProvider.setUrl("http://localhost:3000"); 61 | }]); 62 | ```` -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng-socket", 3 | "main": "ngSocket.js", 4 | "version": "0.1.2", 5 | "authors": [ 6 | "Christopher EnyTC ", 7 | "David Prothero " 8 | ], 9 | "description": "Angular Module for Socket.io", 10 | "keywords": [ 11 | "angular", 12 | "socket.io", 13 | "i18n" 14 | ], 15 | "license": "MIT", 16 | "homepage": "https://github.com/chrisenytc/ng-socket", 17 | "ignore": [ 18 | "**/.*", 19 | "node_modules", 20 | "bower_components", 21 | "lib", 22 | "test", 23 | "tests" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /ngSocket.js: -------------------------------------------------------------------------------- 1 | /* 2 | * ngSocket.js 3 | * https://github.com/chrisenytc/ng-socket 4 | * 5 | * Copyright (c) 2013 Christopher EnyTC, David Prothero 6 | * Licensed under the MIT license. 7 | */ 8 | 9 | // Module Copyright (c) 2013 Michael Benford 10 | 11 | // Module for provide Socket.io support 12 | 13 | (function () { 14 | 'use strict'; 15 | 16 | angular.module('ngSocket', []) 17 | .provider('$socket', socketProvider); 18 | 19 | function socketProvider() { 20 | var url; 21 | 22 | this.setUrl = setUrl; 23 | this.getUrl = getUrl; 24 | this.$get = ['$rootScope', socketFactory]; 25 | 26 | function setUrl(value) { 27 | url = value; 28 | } 29 | 30 | function getUrl() { 31 | return url; 32 | } 33 | 34 | function socketFactory($rootScope) { 35 | var socket; 36 | 37 | var service = { 38 | addListener: addListener, 39 | on: addListener, 40 | once: addListenerOnce, 41 | removeListener: removeListener, 42 | removeAllListeners: removeAllListeners, 43 | emit: emit, 44 | getSocket: getSocket 45 | }; 46 | 47 | return service; 48 | //////////////////////////////// 49 | 50 | function initializeSocket() { 51 | //Check if socket is undefined 52 | if (typeof socket === 'undefined') { 53 | if (url !== 'undefined') { 54 | socket = io.connect(url); 55 | } else { 56 | socket = io.connect(); 57 | } 58 | } 59 | } 60 | 61 | function angularCallback(callback) { 62 | return function () { 63 | if (callback) { 64 | var args = arguments; 65 | $rootScope.$apply(function () { 66 | callback.apply(socket, args); 67 | }); 68 | } 69 | }; 70 | } 71 | 72 | function addListener(name, scope, callback) { 73 | initializeSocket(); 74 | 75 | if (arguments.length === 2) { 76 | scope = null; 77 | callback = arguments[1]; 78 | } 79 | 80 | socket.on(name, angularCallback(callback)); 81 | 82 | if (scope !== null) { 83 | scope.$on('$destroy', function () { 84 | removeListener(name, callback); 85 | }); 86 | } 87 | } 88 | 89 | function addListenerOnce(name, callback) { 90 | initializeSocket(); 91 | socket.once(name, angularCallback(callback)); 92 | } 93 | 94 | function removeListener(name, callback) { 95 | initializeSocket(); 96 | socket.removeListener(name, angularCallback(callback)); 97 | } 98 | 99 | function removeAllListeners(name) { 100 | initializeSocket(); 101 | socket.removeAllListeners(name); 102 | } 103 | 104 | function emit(name, data, callback) { 105 | initializeSocket(); 106 | socket.emit(name, data, angularCallback(callback)); 107 | } 108 | 109 | function getSocket() { 110 | return socket; 111 | } 112 | } 113 | } 114 | 115 | })(); 116 | --------------------------------------------------------------------------------