All Projects → pusher → Pusher Angular

pusher / Pusher Angular

Licence: mit
Pusher Angular Library | [email protected]

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Pusher Angular

project-manager-laravel
Project manager system - PHP and AngularJS
Stars: ✭ 17 (-92.7%)
Mutual labels:  angularjs, pusher
Javastud
Official, Main: This is Core/Advance java example series project. It help to learn java step by step using pdf tutorial provided here and corresponding demo project for the eclipse. Tag: Java Student, Java Stud, Stud Java, StudJava, Java Teachers, Studs Quick Start Guide, Studs Java, Object Oriented Programming, Core Java, Java SE, Java EE, Java Enterprise Edition, Java Blog, Java Articles, Java Web, JSP, Servlet, Maven, Spring, Hibernate, Spring-boot, Spring MVC Web, Angular JS, Angular 2, Java Security, Java CRUD, Java Login Example, File Handling, Multi threading, exception handling, Collection classes, Swing, Database, Date Time, Joda Time, JPA.
Stars: ✭ 220 (-5.58%)
Mutual labels:  angularjs
Webhook.site
⚓️ Easily test HTTP webhooks with this handy tool that displays requests instantly.
Stars: ✭ 2,842 (+1119.74%)
Mutual labels:  pusher
Angular Query Builder
Dynamic query building UI written in Angular.
Stars: ✭ 211 (-9.44%)
Mutual labels:  angularjs
Visualizer
UI-Router state visualizer and transition visualizer
Stars: ✭ 205 (-12.02%)
Mutual labels:  angularjs
Angular Adminlte
AngularjsApp Implementation of renowned Admin-LTE theme (https://github.com/almasaeed2010/AdminLTE) now with many useful directives and features.
Stars: ✭ 214 (-8.15%)
Mutual labels:  angularjs
Angular2 Express Mongoose Gulp Node Typescript
AngularJS 2 (Updated to 4.2.0) Mean Stack application which uses Angular2, Gulp, Express, Node, MongoDB (Mongoose) with Repository Pattern Business Layer
Stars: ✭ 201 (-13.73%)
Mutual labels:  angularjs
Vue Echo
Vue integration for the Laravel Echo library.
Stars: ✭ 229 (-1.72%)
Mutual labels:  pusher
30 Seconds Of Angular
[WIP] Curated collection of Angular snippets that you can understand in 30 seconds or less
Stars: ✭ 221 (-5.15%)
Mutual labels:  angularjs
Electronangular
Sample application using Github Electron and Angular 1.4 with MySql used as database
Stars: ✭ 209 (-10.3%)
Mutual labels:  angularjs
Ionic Starter Template
Reinventing the wheel, again! Sorry Ionic Team... but there are many newbies learning on Youtube!
Stars: ✭ 208 (-10.73%)
Mutual labels:  angularjs
Angular Chartist.js
Angular directive for Chartist.js
Stars: ✭ 206 (-11.59%)
Mutual labels:  angularjs
Mf Geoadmin3
map.geo.admin.ch source code
Stars: ✭ 215 (-7.73%)
Mutual labels:  angularjs
Ui Router
The de-facto solution to flexible routing with nested views in AngularJS
Stars: ✭ 13,738 (+5796.14%)
Mutual labels:  angularjs
Angular5 Seed
Angular5 Seed for Application
Stars: ✭ 222 (-4.72%)
Mutual labels:  angularjs
Angular Material Design Lite
A tiny Angular wrapper for Material Design Lite
Stars: ✭ 203 (-12.88%)
Mutual labels:  angularjs
Angular Examples
A collection of useful directives, filters, and services for Angular JS.
Stars: ✭ 207 (-11.16%)
Mutual labels:  angularjs
Angularjs Csti Scanner
Automated client-side template injection (sandbox escape/bypass) detection for AngularJS.
Stars: ✭ 214 (-8.15%)
Mutual labels:  angularjs
Massautocomplete
Auto Complete for Angularjs applications with a lot to complete
Stars: ✭ 231 (-0.86%)
Mutual labels:  angularjs
Ng Quill
AngularJS Component for Quill rich text editor
Stars: ✭ 223 (-4.29%)
Mutual labels:  angularjs

Pusher Channels AngularJS Library

Build Status Coverage Status

This library is an open source client that allows you to connect to Pusher Channels. It keeps largely the same API as the pusher-js library, with a few differences.

Currently only AngularJS (version 1.x) is supported.

Usage overview

The following topics are covered:

  • Initialisation
  • Subscribing to channels (public, private, encrypted and presence)
  • Accessing Channels
  • Binding to events
    • Globally
    • Per-channel
    • Bind to everything
  • Presence channel members
  • Connection

Initialisation

The first step is to make sure that you have all of the required libraries available to your app before you begin. You'll need something like this in your index.html (or similar):

<!-- AngularJS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.14/angular.min.js"></script>

<!-- pusher-js -->
<script src="//js.pusher.com/4.3/pusher.min.js"></script>

<!-- pusher-angular -->
<script src="//cdn.jsdelivr.net/npm/[email protected]/lib/pusher-angular.min.js"></script>

<!-- pusher-angular (backup CDN)
<script src="//cdnjs.cloudflare.com/ajax/libs/pusher-angular/1.0.0/pusher-angular.min.js"></script>
-->

If you'd like you can use Bower to install pusher-angular using the following command:

bower install pusher-angular --save

With that in place, to start using the AngularJS library you first need to create a Pusher client in exactly the same way that you create one using the pusher-js library, which is as follows:

var pusher = new Pusher(API_KEY);

There are a number of configuration parameters which can be set for the Pusher client, which can be passed as an object to the constructor, i.e.:

var pusher = new Pusher(API_KEY, {
  authEndpoint: "http://example.com/pusher/auth"
});

This is all documented in full here.

When you've created a Pusher client you then need to pass that client to a $pusher object inside your AngularJS controller, service, etc:

angular.module('myApp').controller('MyController', ['$scope', '$pusher',
  function($scope, $pusher) {
    var client = new Pusher(API_KEY);
    var pusher = $pusher(client);
}]);

You can also see here that you need to inject the $pusher service into any controllers, services, etc where you'd like to use Channels in an AngularJS context.

To make the $pusher service available to be used throughout your app you need to ensure that the pusher-angular module is included in your app. You do this by having the following in your app:

angular.module('myApp', ['pusher-angular'])

Note that you can choose to define just one Pusher client, should you prefer, and then use that as the client throughout your AngularJS app. You can do this by simply instantiating a client as follows:

window.client = new Pusher('API_KEY');

and then instantiating instances of $pusher in your AngularJS app using the standard:

var pusher = $pusher(client);

Make sure that you define client before then referencing it in your AngularJS app though.

This is all of the setup required to have Channels available in your AngularJS app. The content below will explain how you can utilise Channels in an AngularJS app.

Subscribing to channels

Public channels

The default method for subscribing to a channel involves invoking the subscribe method of your $pusher object (named pusher throughout the examples provided here):

var my_channel = pusher.subscribe('my-channel');

This returns a Channel object which events can be bound to.

Private channels

Private channels are created in exactly the same way as normal channels, except that they reside in the 'private-' namespace. This means prefixing the channel name:

var my_private_channel = pusher.subscribe('private-my-channel');

Presence channels

Presence channels are again created in exactly the same way as normal channels, except that they reside in the 'presence-' namespace. This means prefixing the channel name:

var my_presence_channel = pusher.subscribe('presence-my-channel');

It is possible to access channels by name, through the channel function:

channel = pusher.channel('private-my-channel');

It is possible to access all subscribed channels through the allChannels function:

var channels = pusher.allChannels();
console.group('Channels - subscribed to:');
for (var i = 0; i < channels.length; i++) {
    var channel = channels[i];
    console.log(channel.name);
}
console.groupEnd();

Encrypted Channels (BETA)

Like private channels, encrypted channels have their own namespace, 'private-encrypted-'. For more information about encrypted channels, please see the docs.

var my_private_encrypted_channel = pusher.subscribe('private-encrypted-my-channel');

Accessing Channels

It is possible to access channels by name, through the channel function:

var my_private_encrypted_channel = pusher.channel('private-my-channel');

It is possible to access all subscribed channels through the allChannels function:

pusher.allChannels().forEach(channel => console.log(channel.name));

Private, presence and encrypted channels will make a request to your authEndpoint (/pusher/auth) by default, where you will have to authenticate the subscription. You will have to send back the correct auth response and a 200 status code.

Binding to events

Events can be bound to at 2 levels, the global, and per channel. They take a very similar form to the way events are handled in jQuery. Note that this is one area in which the API differs to pusher-js. In pusher-angular, a call to bind will return a decorated version of the callback / handler that you pass as a parameter. You will need to assign this to a variable if you wish to unbind the handler from the object in the future. This is explained in the docs for unbinding below.

Global events

You can attach behaviour to these events regardless of the channel the event is broadcast to. The following is an example of an app that binds to new comments from any channel:

var client = new Pusher(API_KEY);
var pusher = $pusher(client);
pusher.subscribe('my-channel');
pusher.bind('new-comment',
  function(data) {
    // add comment into page
  }
);

Per-channel events

These are bound to a specific channel, and mean that you can reuse event names in different parts of your client application. The following might be an example of a stock tracking app where several channels are opened for different companies:

var client = new Pusher(API_KEY);
var pusher = $pusher(client);
var my_channel = pusher.subscribe('my-channel');
my_channel.bind('new-price',
  function(data) {
    // update with new price
  }
);

Binding to everything

It is possible to bind to all events at either the global or channel level by using the method bind_all. This is used for debugging, but may have other utilities.

Unbind event handlers

Remove previously-bound handlers from an object. Only handlers that match all of the provided arguments (eventName, handler or context) are removed.

var handler = function() { console.log('testing'); };
var decoratedHandler = my_channel.bind('new-comment', handler);

channel.unbind('new-comment', decoratedHandler); // removes just `decoratedHandler` for the `new-comment` event
channel.unbind('new-comment'); // removes all handlers for the `new-comment` event
channel.unbind(null, decoratedHandler); // removes `decoratedHandler` for all events
channel.unbind(null, null, context); // removes all handlers for `context`
channel.unbind(); // removes all handlers on `channel`

The same API applies to unbinding handlers from the client object.

Presence channel members

All presence channels have a members object that contains information about all of the members in the channel. More specific information can be found in the Channels docs.

In this library the members object is setup to automatically reflect changes in members of the channel. That means if you had the following code in a controller:

angular.module('myApp').controller('MyController', ['$scope', '$pusher',
  function($scope, $pusher) {
    var client = new Pusher(API_KEY);
    var pusher = $pusher(client);

    var presence = pusher.subscribe('presence-test');
    $scope.members = presence.members;
}]);

and the following HTML in your view:

<div ng-controller='MyController'>
  {{members.count}}
  {{members.members}}
</div>

your view would update the members count and the members list whenever there was a member added or removed from the channel.

Connection

You can bind to specific events on your $pusher objects connection, such as state_change, using the following code:

var client = new Pusher(API_KEY);
var pusher = $pusher(client);

pusher.connection.bind('state_change', function (states) {
  // var previous = states.previous ...
})

Similarly to the client and channel, you can also bind to all events on a connection using the bind_all method. That looks like this:

var client = new Pusher(API_KEY);
var pusher = $pusher(client);

pusher.connection.bind_all(function (eventName, data) {
  // if (eventName == 'state_change') { ...
})

Contributing

If you'd like to contribute to the library then fork it, hack away at it, improve it, test it, and then make a pull request.

You can make sure that your changes / improvements don't break anything by running the unit tests. To run them just run karma start and then you can go ahead and make changes to the library and the tests and watch the tests run again to see if they're still passing.

You can generate the minimized file as following:

uglifyjs lib/pusher-angular.js -m -o lib/pusher-angular.min.js

Support

If you have questions that aren't answered here or in the code's inline documentation then feel free to create an issue describing your problem.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].