Skip to content

Instantly share code, notes, and snippets.

@fergalmoran
Last active August 21, 2018 03:52
Show Gist options
  • Save fergalmoran/b99a30b331edfa39bfdf7d405d7ea31c to your computer and use it in GitHub Desktop.
Save fergalmoran/b99a30b331edfa39bfdf7d405d7ea31c to your computer and use it in GitHub Desktop.
Angular 2 Pusher Service
import * as ng from '@angular/core';
import {PusherService} from '../../services/pusher.service'
@ng.Component({
selector: 'home',
template: require('./home.html')
})
export class Home {
constructor(private pusher: PusherService) {
console.log('Home:constructor');
pusher.messages.subscribe(data => {
console.log(data);
});
}}
declare var Pusher: any;
import { Injectable, Output, EventEmitter } from '@angular/core';
import {Observable} from 'rxjs/Observable'
import {BehaviorSubject} from "rxjs/Rx";
import {List} from 'immutable';
@Injectable()
export class PusherService {
private pusher: any;
private channels: any[];
private _messages: BehaviorSubject<List<string>> = new BehaviorSubject(List([]));
public messages: Observable<List<string>> = this._messages.asObservable();
constructor() {
console.log('PusherService', 'constructor');
this.pusher = new Pusher('<your_key>', {
cluster: 'eu',
encrypted: true
});
this.pusher.logToConsole = true;
this.channels = [];
var channel = this.pusher.subscribe('test_channel');
channel.bind('my_event', (data) => {
console.log(data.message);
this._messages.next(data.message);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment