pubnub / pubnub-angular2

Other
33 stars 13 forks source link

Get messages in history not working (or at least for me) #23

Closed Richi-kun closed 7 years ago

Richi-kun commented 7 years ago
/ .... /

@Injectable()
export class ChatService {
  public uuid: string;
  public message: string;
  public messages = [];
  public channel: string[];

  constructor(private pubnub: PubNubAngular) {
    this.channel = ['test'];
    this.uuid = 'Rich';

    this.pubnub.init({
      publishKey: 'pub-key',
      subscribeKey: 'sub_key',
      ssl: true
    });

    this.pubnub.addListener({
      message: (m) => {
        this.messages.push(m.message);
      }
    });

    this.pubnub.subscribe({
      channels: this.channel,
      withPresence: true,
      triggerEvents: ['message', 'presence'],
      autoLoad: 100
    });

    ///// Somehow this doesn't return anything, dont know why

    this.messages = this.pubnub.getMessages(this.channel);

    //// So I had to manually get the messages from the history

    this.pubnub.history({ channel: this.channel }).then((response) => {
      for (let m in response.messages) {
        let msg: object = {
          origin: response.messages[m].entry.origin,
          timetoken: response.messages[m].entry.timetoken,
          content: response.messages[m].entry.content,
          sender: response.messages[m].entry.sender
        }
        this.messages.push(msg);
      }
    });

  }

/.... /

}
ManuelFernando commented 7 years ago

Hi @Richi-kun I think you forgot to assign pubnub parameter to this.pubnub constructor(private pubnub: PubNubAngular) { this.pubnub = pubnub; … }

Richi-kun commented 7 years ago

@ManuelFernando Still doesn't work, I even tried using the getMessage() callback and logging it in the console, turns out its still empty :(

ManuelFernando commented 7 years ago

@Richi-kun can you say to me what version are you using? and please inside the listener add the status event. this is going to say to us what is the state of the connection

Richi-kun commented 7 years ago

@ManuelFernando, i fixed it dunno what I did lol 😆

Richi-kun commented 7 years ago

@ManuelFernando, anyways thank you very much for the attention 😄

ManuelFernando commented 7 years ago

I glad that you have found the trouble