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

Payload issue after upgrading from 3.1.0 #190

Open
kacperd opened this issue Oct 1, 2024 · 9 comments
Open

Payload issue after upgrading from 3.1.0 #190

kacperd opened this issue Oct 1, 2024 · 9 comments

Comments

@kacperd
Copy link

kacperd commented Oct 1, 2024

Hello!

In our project, I noticed the issue with messages payload after upgrading to the SDK version 7.0.0 (previously, we were using version 3.1.0). The problem is that the payload no longer represents the correct JSON and our parsing started to fail.

Here's the debugged value of the payload that we received in SDK 7.0.0:

                  ▿ concretePayload : action","data":{"type":"chat_updated"}}
                  ▿ value : AnyJSONType
                       - string : "action\",\"data\":{\"type\":\"chat_updated\"}}"

It looks like the beginning of the json response was removed and it's no longer interpreted as a valid JSON.

In version 3.1.0, the payload was a correct JSON.

@jguz-pubnub
Copy link
Contributor

Hi @kacperd, could you show an example how do you publish a message with your custom payload?

@kacperd
Copy link
Author

kacperd commented Oct 1, 2024

Unfortunately, I don't have access to the project that publishes messages 😞

@jguz-pubnub
Copy link
Contributor

Here's how you could do it. Let's assume you have your custom struct (or any other type) conforming to our JSONCodable protocol:

struct YourCustomStruct: JSONCodable {
  var action: String
  var data: [String: String]
}

Then, here's how you could use publish and subscribe methods:

// Your configured PubNub instance
var pubNub: PubNub = ...
// Assumes you store a strong reference to the listener object
var listener = SubscriptionListener()

// Publish
pubNub.publish(
  channel: "your-channel",
  message: YourCustomStruct(action: "action", data: ["type": "chat_updated"])
) {
  switch $0 {
  case let .success(tt):
    debugPrint("Message successfully published at \(tt) timetoken")
  case let .failure(error):
    debugPrint("Failed to publish a message due to error \(error)")
  }
}

listener.didReceiveMessage = {
  if let decodedPayload = try? $0.payload.decode(YourCustomStruct.self) {
    debugPrint("Payload successfuly decoded to YourCustomStruct")
    // Proceed with a decodedPayload object
  } else {
    debugPrint("Failed to decode a payload")
  }
}

@kacperd
Copy link
Author

kacperd commented Oct 2, 2024

Thanks for your help @jguz-pubnub. I think I was not precise enough when I first described the issue. The problem we have is that the .payload is not correct. Compare the difference in payload while using two different versions of SDK.

Here's the payload we get from PubNub SDK in version 3.1.0

 ▿ concretePayload : ["data": ["type": "chat_updated"], "event": "trade-action"]
    ▿ value : AnyJSONType
      ▿ dictionary : 2 elements
        ▿ 0 : 2 elements
          - key : "data"
          ▿ value : AnyJSONType
            ▿ dictionary : 1 element
              ▿ 0 : 2 elements
                - key : "type"
                ▿ value : AnyJSONType
                  - string : "chat_updated"
        ▿ 1 : 2 elements
          - key : "event"
          ▿ value : AnyJSONType
            - string : "trade-action"

Here's the debugged value of the payload that we received in SDK 7.0.0:


 ▿ concretePayload : action","data":{"type":"chat_updated"}}
     ▿ value : AnyJSONType
        - string : "action\",\"data\":{\"type\":\"chat_updated\"}}"

And decoding of the latter fails with:

typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found a string instead.", underlyingError: nil))

The payload in 7.0.0 is missing the "event": "trade-action".

What could be the reason for this difference?

@jguz-pubnub
Copy link
Contributor

I did a quick test on the newest 8.0.0 version, but it should also work for 7.0.0. We haven't changed anything with regards serialization:

let messageToPublish = ["data": ["type": "chat_updated"], "event": "trade-action"] as [String : JSONCodable]
    
pubNub.publish(
  channel: "your-channel-name",
  message: AnyJSON(messageToPublish),
  completion: { result in
      debugPrint(result)
  }
)

Then, I was able to receive expected payload and decode it into raw dictionary:

listener.didReceiveMessage = { message in
   let decodedPayload = try? message.payload.decode([String: AnyJSON].self)
   let event = decodedPayload?["event"]?.stringOptional
   let data = decodedPayload?["data"]?.dictionaryOptional
   
   debugPrint("event: \(String(describing: event)), data: \(String(describing: data))")
}

Another question is, do you publish a Dictionary you mentioned from another endpoint that uses PubNub Swift SDK? Or is it a message from developer console or any other SDK like Java, Kotlin, JavaScript, etc?

@kacperd
Copy link
Author

kacperd commented Oct 2, 2024

This dictionary is not published by PubNub Swift SDK. It's published by some backend service. I don't have implementation details, but I know that nothing has changed in the way the messages are published.

@jguz-pubnub
Copy link
Contributor

jguz-pubnub commented Oct 3, 2024

Do you use another PubNub SDK on the server side or do you encode a message yourself and manually invoke our rest API and its publish method? I will test the example dictionary payload (similar to yours) for both 3.1.0 and the newest version and let you know. But it would be great to test the real message (without any confidential data) you trigger from the server side

@kacperd
Copy link
Author

kacperd commented Oct 7, 2024

From the information I got, we use PubNub PHP SDK version 4.1.7 on the server.

@jguz-pubnub
Copy link
Contributor

jguz-pubnub commented Oct 11, 2024

Could you update PHP SDK used on the server side to the newer value? The one you use is quite obsolete. I pasted your payload into our PubNub Developer Console and Debugger (you can try it out by filling up the left panel with your keys, channel and user id), published a message and I had no problems with parsing for both 3.1.0 and 7.0.0 Swift SDK versions. Here's the example payload I mentioned:

{ "data": { "type": "chat_updated" }, "event": "trade-action" }

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