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

Sample doesn't show how to deal with chunked messages #2

Open
webOS101 opened this issue Sep 17, 2019 · 4 comments
Open

Sample doesn't show how to deal with chunked messages #2

webOS101 opened this issue Sep 17, 2019 · 4 comments

Comments

@webOS101
Copy link

webOS101 commented Sep 17, 2019

When large messages are posted into a gitter channel they can be chunked before sending to the REST API. In this case, the message must be reassembled before parsing. It would be helpful to developers to call this out in the sample code. Even better, to show to the proper way to detect end-of-message.

@MadLittleMods
Copy link
Member

@webOS101 Pull request welcome!

@webOS101
Copy link
Author

Understood... but, I'm actually not sure what the proper way to resolve this is. I resolved it myself by try/catch around a JSON.parse(msg) and building up the chunk if it fails, but that seems horrific. I didn't have time to look for a more elegant solution

@MadLittleMods
Copy link
Member

I think you may be able to delimit based on \n

The JSON stream returns messages as JSON objects that are delimited by newline (\n). Space + newline (\n) are sent as periodic "keep-alive" messages to tell clients and NAT firewalls that the connection is still alive during low message volume periods.

https://developer.gitter.im/docs/streaming-api

But you can also look to see if the last character is a }(maybe }\n) as a way to tell it is a full message


@webOS101
Copy link
Author

webOS101 commented Sep 23, 2019

There may be some assumptions about whether more than one object can be sent in a single stream that need some testing. I may give it a try to see if I can come up with something robust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants