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 SDK + Access Manager #180

Open
manfe opened this issue Oct 8, 2024 · 6 comments
Open

Chat SDK + Access Manager #180

manfe opened this issue Oct 8, 2024 · 6 comments

Comments

@manfe
Copy link

manfe commented Oct 8, 2024

When Access Manager is enabled I am initializing the Chat SDK with:

const chatInstance = await Chat.init({
    publishKey: process.env.NEXT_PUBLIC_PUBNUB_PUBLISH_KEY,
    subscribeKey: process.env.NEXT_PUBLIC_PUBNUB_SUBSCRIBE_KEY,
    userId: "user-id-example",
    authKey: "my-token" // This authKey is the Access Manager authorized token
})

But if I need to update the access manager token after a period of time, I noticed that the pubnub is wrapped into the sdk property.

Is it ok to update the Access Manager token using the method below?

chatInstance.sdk.setToken("my-new-token")

Or there is other recommended way to do it?

Also if I need to listen 403 errors I continue listening on that sdk property?

@piotr-suwala
Copy link
Contributor

Hello @manfe!

Yes, it's okay to update the Access Manager token using setToken on the sdk property. However we noticed that a stale value will be used in the setLastReadMessageTimetoken and we need to fix it soon. You should be good to go with setToken.

Regarding your second question: you can use code like this if you use React:

  useEffect(() => {
    if (!chat) {
      return
    }

    const listeners = {
      status(statusEvent: Pubnub.StatusEvent) {
        if (
          statusEvent.category === "PNNetworkDownCategory" ||
          statusEvent.category === "PNNetworkIssuesCategory" ||
          statusEvent.category === "PNConnectionErrorCategory"
        ) {
        }
        // do something
      },
    }

    chat.sdk.addListener(listeners)

    return () => {
      chat.sdk.removeListener(listeners)
    }
  }, [chat])

@manfe
Copy link
Author

manfe commented Oct 14, 2024

@piotr-suwala how are you able to import Pubnub only using "@pubnub/chat": "^0.8.2" ?

Because I am not seeing it exported.

@piotr-suwala
Copy link
Contributor

You're right. This is not exported.

You can replace

status(statusEvent: Pubnub.StatusEvent)

with

status(statusEvent: { category: string })

@manfe
Copy link
Author

manfe commented Oct 16, 2024

@piotr-suwala another issue that I found.

If I have listeners to a channel or messages and the token is updated, those messages will keep the old token and while we don't stop listen and listen again (re-render all messages) with new listeners will not be able to toggle reaction on them or take any action with the stale token.

@piotr-suwala
Copy link
Contributor

@manfe Did you use chatInstance.sdk.setToken("my-new-token") when your token is updated? Our PN Messages do not contain the AM token on their own.

@manfe
Copy link
Author

manfe commented Oct 23, 2024

@piotr-suwala yes, I was using this approach, but old messages couldn't have any kind o reaction.

So I had to initialize the chat again when a new token arrive.

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