I want to add PubNub to an angular2-cli project. The problem is with the linking; however, I followed the instructions of the pubnub-angular2 package on npmjs.com.
When I try to load it in the browser, the error message is this:
EXCEPTION: PubNub is not in global scope. Ensure that pubnub.js v4 library is included before the angular adapter
In the app.module.ts, I have the following:
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';
import {PubNubAngular} from 'pubnub-angular2';
import {AppComponent} from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [PubNubAngular],
bootstrap: [AppComponent]
})
export class AppModule {}
In the app.component.ts, I have the following code:
import {Component, Injectable, Class} from '@angular/core';
import {PubNubAngular} from "pubnub-angular2";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Pubnub Chat Service';
private channel = 'chat-demo-app';
constructor(private pubnubService: PubNubAngular) {
pubnubService.init({
publishKey: 'pub-c-***',
subscribeKey: 'sub-c-***'
});
}
sendMessage(message: string): void {
this.pubnubService.publish({
channel: this.channel,
message: message
});
}
}
Note that if I remove the import in the AppComponent, the component does not see the PubNubAngular provider.
Thanks in advance.