Last active
August 21, 2018 03:52
-
-
Save fergalmoran/b99a30b331edfa39bfdf7d405d7ea31c to your computer and use it in GitHub Desktop.
Angular 2 Pusher Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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