SwiftPhoenixClient icon indicating copy to clipboard operation
SwiftPhoenixClient copied to clipboard

Access raw message data

Open ream88 opened this issue 3 years ago • 2 comments

I'm currently rewriting an APP of ours from a classic HTTP-based API to using web sockets. We already have a ton of different models and decoders for that reason, and I would love to reuse as much code as possible. Therefore my question: Is there a way to get the raw payload from a Message without parsing it as JSON?

ream88 avatar Feb 06 '22 21:02 ream88

You can implement your own decoder (see Defaults.swift) but the client is expecting that decoder will return [Any] which will be parsed into a Message. If you don't decode the Data into the correct format then you wont receive any messages to your channels

guard
      let data = rawMessage.data(using: String.Encoding.utf8),
      let json = decode(data) as? [Any?],
      let message = Message(json: json)
      else {
        self.logItems("receive: Unable to parse JSON: \(rawMessage)")
        return }

Do your models convert Data to MyModel and you're wanting to convert Message.payload into MyModel without using JSON serialization?

dsrees avatar Feb 07 '22 14:02 dsrees

Do your models convert Data to MyModel and you're wanting to convert Message.payload into MyModel without using JSON serialization?

Basically yes, I already have my decoding logic written, and it's accepting Data and converts it to MyModel. Serializing [Any] back to JSON to then convert it to MyModel just feels wrong, therefore I think some kind of low-level API to access the raw Data would be awesome.

ream88 avatar Feb 07 '22 21:02 ream88