davidstump / SwiftPhoenixClient

Connect your Phoenix and iOS applications through WebSockets!
MIT License
506 stars 146 forks source link

Access raw message data #215

Open ream88 opened 2 years ago

ream88 commented 2 years ago

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?

dsrees commented 2 years ago

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?

ream88 commented 2 years ago

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.