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

PubSub #55

Closed
SionoiS opened this issue Jul 9, 2020 · 9 comments · Fixed by #94
Closed

PubSub #55

SionoiS opened this issue Jul 9, 2020 · 9 comments · Fixed by #94

Comments

@SionoiS
Copy link
Contributor

SionoiS commented Jul 9, 2020

Hello!

I tried to send and receive messages locally but sending "hello world" I received "aGVsbG8gd29ybGQ=".
If I use the CLI ipfs pubsub sub It work as expected which makes me thing there's a bug somewhere.

I'll look around and see if I can fix it.

Otherwise great API!

@jcaesar
Copy link
Contributor

jcaesar commented Jul 9, 2020

I guess this is in base64 because the answer is returned in JSON, and that can't handle arbitrary binary data?

@SionoiS
Copy link
Contributor Author

SionoiS commented Jul 9, 2020

{
  "data": "<base64-string>",
  "from": "<base64-string>",
  "seqno": "<base64-string>",
  "topicIDs": [
    "<string>"
  ]
}

edit: I'm blind...

I tried decoding the data value with multibase and it failed so IDK...

Also, notice the = sign at the end, no multibase base have that char in their alphabet.

@SionoiS
Copy link
Contributor Author

SionoiS commented Jul 9, 2020

Ok I was wrong there's no bug...

I decoded the string but had to add a 'm' at the start to signify to multibase that it was base64 string.

@SionoiS SionoiS closed this as completed Jul 9, 2020
@jcaesar
Copy link
Contributor

jcaesar commented Aug 20, 2020

I don't know... I think base64 is part of the format of the request, maybe the library should decode it.

@SionoiS
Copy link
Contributor Author

SionoiS commented Aug 20, 2020

I don't know... I think base64 is part of the format of the request, maybe the library should decode it.

The docs tell us that it should be base64.

In reality it's base64 with padding. I used multibase crate with Base64Pad as setting.

+1 for having the lib decode it, it's a little confusing right now.

@ferristseng ferristseng reopened this Aug 20, 2020
@Tails
Copy link

Tails commented Sep 20, 2020

I had trouble here too, as did this StackOverflow poster. I was confused too. I started trying to decode the 'from' field but it kept failing. Until I realized that the decoded 'from' result is actually binary, and that the 'data' and 'seqno' were fine. This worked for me:

String::from_utf8(multibase::decode(format!("M{}", s)).unwrap().1)

@SionoiS
Copy link
Contributor Author

SionoiS commented Sep 21, 2020

I use this for the from field

let encoded = match response.from.as_ref()?;

let decoded = Base::decode(&Base::Base64Pad, encoded).expect("Decoding sender failed");

let cid = Cid::try_from(decoded).expect("CID from decoded sender failed");

and this for message

let encoded = response.data.as_ref()?;

let decoded = Base::decode(&Base::Base64Pad, encoded).expect("Decoding message failed");

let msg_str = match str::from_utf8(&decoded) {
        Ok(data) => data,
        Err(_) => {
            eprintln!("Chat message invalid UTF-8");
            return None;
        }
};

I hope it helps!

@Tails
Copy link

Tails commented Sep 24, 2020

Does anyone know what the topicIds variable contains? Is it the topic ids when subscribing to a wildcard? Or is it the topic ids that a peer is subscribed to in total?

@SionoiS
Copy link
Contributor Author

SionoiS commented Sep 24, 2020

Test it but I'm pretty sure it's the topic the message was send on.

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

Successfully merging a pull request may close this issue.

4 participants