0

I'm working with other company to sending and receiving data in real time.

They give me a websocket URL like this: ws://domain.com/socket.io/?EIO=4&transport=websocket and I need to connect to it for transfer data.

The problem is they said that data is encoded with base64 string, and when I received data, I couldn't completely decode it. When I decode data by atob() function it show readable string but it had some special characters which I couldn't understand.

For example, here is one of the response messages in websocket: h6d1c2VyS2V50Rqdom5v0wAAAYSBGgjtp3ZlcnNpb26mMS4xMy4wqXNlc3Npb25JZLAxNjY4NjEzMDI0NDM0NDM0o3NpZ6Cmc3luY0lkAKRib2R5gad1c2VyS2V50Rqd

When I decode base64 string, the result is: \x87§userKeyÑ\x1A\x9D¢noÓ\x00\x00\x01\x84\x81\x1A\bí§version¦1.13.0©sessionId°1668613024434434£sig ¦syncId\x00¤body\x81§userKeyÑ\x1A\x9D

I simply decode it by atob(encodedData), and get back result like this. I don't know what problem is. How should I fix this?

1
  • FYI : that base64 string you provided has errors, and it contains invalid characters. Even Node.js' Buffer.from(s, 'base64') (as suggested in the answer below) produces the same corrupt string than atob. Commented Nov 16, 2022 at 16:38

1 Answer 1

0

Using the atob function is deprecated and I suggest you this little snippet to decode base64 encoded strings:

const encoded = "SOME_BASE64_STRING"
const decoded = new Buffer(encoded , "base64").toString()

Edit: For client-side usage without the browserify module I recommend you to use this package.

Sign up to request clarification or add additional context in comments.

1 Comment

This is only true for Node.js. atob and btoa are not deprecated in the standard, and the Buffer class and isn't even available outside of node.js

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.