Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat connect/disconnect callbacks #181

Closed
manfe opened this issue Oct 14, 2024 · 3 comments
Closed

Chat connect/disconnect callbacks #181

manfe opened this issue Oct 14, 2024 · 3 comments

Comments

@manfe
Copy link

manfe commented Oct 14, 2024

Sorry to tag you directly piotr-suwala

But this is something really urgent.

The current implementation of connect on channel entity at least for me is not working as expected.

  /*
   * Streaming messages
   */
  connect(callback: (message: Message) => void) {
    const listener = {
      message: (event: MessageEvent) => {
        if (event.channel !== this.id) return
        const getMessageResponseBody =
          this.chat.config.customPayloads.getMessageResponseBody || defaultGetMessageResponseBody
        if (getMessageResponseBody(event).type !== "text") return
        callback(Message.fromDTO(this.chat, event))
      },
    }

    const removeListener = this.chat.addListener(listener)
    const unsubscribe = this.chat.subscribe(this.id)

    return () => {
      removeListener()
      unsubscribe()
    }
  }
export function defaultGetMessageResponseBody(messageParams: MessageDTOParams) {
  if (typeof messageParams.message === "string") {
    return {
      type: MessageType.TEXT,
      text: messageParams.message,
      files: [],
    }
  }
  return messageParams.message
}

Adding some debugs to defaultGetMessageResponseBody function:

    console.debug("e.message =>", e.message)
    console.debug("typeof e.message =>", typeof e.message)
    console.debug(`"string" == typeof e.message =>`, "string" == typeof e.message);

I am getting:

index.es.js:260: e.message => {text: 'example', timetoken: 1728918366738}
index.es.js:261: typeof e.message => object
index.es.js:262: "string" == typeof e.message => false

Hence, all my text messages it's returning on line:

return messageParams.message

that it's an object and do not include type property that is expected in this check on connect method:

if (getMessageResponseBody(event).type !== "text") return
@manfe
Copy link
Author

manfe commented Oct 14, 2024

To unblock me I am removing the line

if (getMessageResponseBody(event).type !== "text") return

So I can receive all messages regardless the type of it

@piotr-suwala
Copy link
Contributor

Hello @manfe

Can you construct your own "getMessageResponseBody" as in this test? https://github.com/pubnub/js-chat/blob/master/lib/tests/message.test.ts#L1656

Something like:

Chat.init({
  customPayloads: {
    getMessageResponseBody: (messageParams: MessageDTOParams) => {
            return {
              text: messageParams.message.text,
              type: MessageTypes.Text,
              files: messageParams.message.body.files,
            }
          },
  }
})

@manfe
Copy link
Author

manfe commented Oct 15, 2024

That worked, thanks!

@manfe manfe closed this as completed Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants