• This repository has been archived on 06/Jul/2022
  • Stars
    star
    233
  • Rank 165,958 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pusher Angular Library | owner=@leesio

This library is no longer supported

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/pusher-angular@latest/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.

More Repositories

1

pusher-js

Pusher Javascript library
JavaScript
1,970
star
2

atom-pair

An Atom package that allows for epic pair programming
JavaScript
1,454
star
3

pusher-http-php

PHP library for interacting with the Pusher Channels HTTP API
PHP
1,355
star
4

pusher-http-ruby

Ruby library for Pusher Channels HTTP API
Ruby
659
star
5

libPusher

An Objective-C interface to Pusher Channels
C
409
star
6

pusher-http-laravel

[DEPRECATED] A Pusher Channels bridge for Laravel
PHP
405
star
7

pusher-http-python

Pusher Channels HTTP API library for Python
Python
368
star
8

k8s-spot-rescheduler

Tries to move K8s Pods from on-demand to spot instances
Go
311
star
9

pusher-websocket-java

Pusher Channels client library for Java targeting general Java and Android
Java
302
star
10

pusher-websocket-swift

Pusher Channels websocket library for Swift
Swift
267
star
11

build-a-slack-clone-with-react-and-pusher-chatkit

In this tutorial, you'll learn how to build a chat app with React, complete with typing indicators, online status, and more.
JavaScript
235
star
12

pusher-http-go

Pusher Channels HTTP API library for Go
Go
196
star
13

k8s-spot-termination-handler

Monitors AWS for spot termination notices when run on spot instances and shuts down gracefully
Makefile
118
star
14

NWWebSocket

A WebSocket client written in Swift, using the Network framework from Apple.
Swift
112
star
15

go-interface-fuzzer

Automate the boilerplate of fuzz testing Go interfaces | owner: @willsewell
Go
110
star
16

pusher-http-dotnet

.NET library for interacting with the Pusher HTTP API
C#
109
star
17

k8s-auth-example

Example Kubernetes Authentication helper. Performs OIDC login and configures Kubectl appropriately.
Go
108
star
18

pusher-websocket-dotnet

Pusher Channels Client Library for .NET
C#
107
star
19

faros

Faros is a CRD based GitOps controller
Go
100
star
20

backbone-todo-app

JavaScript
92
star
21

chatkit-client-js

JavaScript client SDK for Pusher Chatkit
JavaScript
89
star
22

quack

In-Cluster templating for Kubernetes manifests
Go
70
star
23

pusher-channels-flutter

Pusher Channels client library for Flutter targeting IOS, Android, and WEB
Dart
67
star
24

websockets-from-scratch-tutorial

Tutorial that shows how to implement a websocket server using Ruby's built-in libs
Ruby
60
star
25

backpusher

JavaScript
54
star
26

push-notifications-php

Pusher Beams PHP Server SDK
PHP
54
star
27

chatkit-android

Android client SDK for Pusher Chatkit
Kotlin
54
star
28

pusher-websocket-react-native

React Native official Pusher SDK
TypeScript
53
star
29

django-pusherable

Real time notification when an object view is accessed via Pusher
Python
52
star
30

notify

Ruby
51
star
31

cli

A CLI for Pusher (beta)
Go
50
star
32

k8s-spot-price-monitor

Monitors the spot prices of instances in a Kubernetes cluster and exposes them as prometheus metrics
Python
44
star
33

chatkit-command-line-chat

A CLI chat, built with Chatkit
JavaScript
41
star
34

pusher-http-java

Java client to interact with the Pusher HTTP API
Java
40
star
35

chatkit-swift

Swift SDK for Pusher Chatkit
Swift
40
star
36

electron-desktop-chat

A desktop chat built with React, React Desktop and Electron
JavaScript
39
star
37

push-notifications-web

Beams Browser notifications
JavaScript
38
star
38

crank

Process slow restarter
Go
37
star
39

pusher-websocket-android

Library built on top of pusher-websocket-java for Android. Want Push Notifications? Check out Pusher Beams!
Java
35
star
40

chameleon

A collection of front-end UI components used across Pusher ✨
CSS
35
star
41

chatkit-server-php

PHP SDK for Pusher Chatkit
PHP
35
star
42

cide

Isolated test runner with Docker
Ruby
33
star
43

push-notifications-swift

Swift SDK for the Pusher Beams product:
Swift
33
star
44

pusher-phonegap-android

JavaScript
30
star
45

push-notifications-python

Pusher Beams Python Server SDK
Python
30
star
46

pusher-websocket-unity

Pusher Channels Unity Client Library
C#
27
star
47

hacktoberfest

24
star
48

laravel-chat

PHP
22
star
49

push-notifications-android

Android SDK for Pusher Beams
Kotlin
21
star
50

push-notifications-node

Pusher Beams Node.js Server SDK
JavaScript
20
star
51

pusher-test-iOS

iOS app for developers to test connections to Pusher
Objective-C
19
star
52

push-notifications-ruby

Pusher Beams Ruby Server SDK
Ruby
19
star
53

chatkit-server-node

Node.js SDK for Pusher Chatkit
TypeScript
16
star
54

rack-headers_filter

Remove untrusted headers from Rack requests | owner=@zimbatm
Ruby
15
star
55

pusher-test-android

Test and diagnostic app for Android, based on pusher-java-client
Java
14
star
56

pusher-realtime-tfl-cameras

Realtime TfL Traffic Camera API, powered by Pusher
JavaScript
14
star
57

buddha

Buddha command execution and health checking | owner: @willsewell
Go
14
star
58

chatkit-server-go

Chatkit server SDK for Golang
Go
13
star
59

pusher-channels-auth-example

A simple server exposing a pusher auth endpoint
JavaScript
13
star
60

pusher-platform-js

Pusher Platform client library for browsers and react native
TypeScript
13
star
61

stronghold

[DEPRECATED] A configuration service | owner: @willsewell
Haskell
12
star
62

pusher-twilio-example

CSS
12
star
63

prom-rule-reloader

Watches configmaps for prometheus rules and keeps prometheus in-sync
Go
12
star
64

sample-chatroom-ios-chatkit

How to make an iOS Chatroom app using Swift and Chatkit
PHP
11
star
65

electron-desktop-starter-template

JavaScript
11
star
66

chatkit-server-ruby

Ruby server SDK for Chatkit
Ruby
11
star
67

realtime-visitor-tracker

Realtime location aware visitor tracker for a web site or application
PHP
11
star
68

push-notifications-server-java

Pusher Beams Java Server SDK
Kotlin
10
star
69

android-slack-clone

Android chat application, built with Chatkit
Kotlin
10
star
70

filtrand

JavaScript
10
star
71

vault

Front-end pattern library
Ruby
9
star
72

push-notifications-go

Pusher Beams Go Server SDK
Go
9
star
73

pusher-platform-android

Pusher Platform SDK for Android
Kotlin
9
star
74

git-store

Go git abstraction for use in Kubernetes Controllers
Go
8
star
75

pusher-platform-swift

Swift SDK for Pusher platform products
Swift
8
star
76

realtime_survey_complete

JavaScript
8
star
77

docs

The all new Pusher docs, powered by @11ty and @vercel
CSS
8
star
78

push-notifications-server-swift

Pusher Beams Swift Server SDK
Swift
8
star
79

pusher-python-rest

Python client to interact with the Pusher REST API. DEPRECATED in favour of https://github.com/pusher/pusher-http-python
Python
8
star
80

real-time-progress-bar-tutorial

Used inthe realtime progress bar tutorial blog post - http://blog.pusher.com
JavaScript
7
star
81

pusher-channels-chunking-example

HTML
7
star
82

pusher-http-swift

Swift library for interacting with the Pusher Channels HTTP API
Swift
7
star
83

feeds-client-js

JS client for Pusher Feeds
JavaScript
6
star
84

pusher-test

Simple website which allows manual testing of pusher-js versions
JavaScript
6
star
85

java-websocket

A fork of https://github.com/TooTallNate/Java-WebSocket | owner=@zmarkan
HTML
6
star
86

bridge-troll

A Troll that ensures files don't change
Go
5
star
87

navarchos

Node replacing controller
Go
5
star
88

realtime-notifications-tutorial

Create realtime notifications in minutes, not days =)
4
star
89

pusher-socket-protocol

Protocol for pusher sockets
HTML
4
star
90

icanhazissues

Github issues kanban
JavaScript
4
star
91

textsync-server-node

[DEPRECATED] A node.js library to simplify token generation for TextSync authorization endpoints.
TypeScript
4
star
92

pusher_tutorial_realtimeresults

JavaScript
3
star
93

pusher-js-diagnostics

JavaScript
3
star
94

react-rest-api-tutorial

Accompanying tutorial for consuming RESTful APIs in React
CSS
3
star
95

testing

Configuration for Pusher's Open Source Prow instance
Go
3
star
96

feeds-server-node

The server Node SDK for Pusher Feeds
JavaScript
3
star
97

spacegame_example

Simple example of a space game using node.js and Pusher
JavaScript
3
star
98

chatkit-quickstart-swift

A project to get started with Chatkit.
Swift
2
star
99

pusher-whos-in

Ruby
2
star
100

chatkit-android-public-demo

This will hold the demo app for chatkit android. Owner: @daniellevass
Kotlin
2
star