Streaming API gets error while event:delete was streamed
obeliskgolem opened this issue · 2 comments
obeliskgolem commented
Hello there,
I was using mastodon ruby streaming api today and I found Streaming API was not stable. It yields the following error while receiving new events.
/Users/XXX/.rbenv/versions/2.3.6/lib/ruby/2.3.0/json/common.rb:156:in `parse': 784: unexpected token at '100105350338424078' (JSON::ParserError)
When I tracked the stream using curl, I found out that the stream not only pushed event:update, but also event:delete. Such kind of JSON response will cause ParseError in Streaming API.
event: delete
data: 100105350338424078
Is there any way to get rid of this? I am a newbie, both to mastodon and ruby. Please help! Thanks!
BTW my ruby code attached:
streaming_client.stream("public/local") do |stream_toot|
next if !stream_toot.kind_of?(Mastodon::Status)
puts stream_toot.content
obeliskgolem commented
I've found a workaround which need modification of source code "streaming/response.rb":
def on_body(data)
@tokenizer.extract(data).each do |line|
has_data = @match.match(line)
next if has_data.nil?
type = has_data[1]
data = has_data[2]
next if !(type == "update") # added a check before parsing JSON
@block.call(type, JSON.parse(data))
end
obeliskgolem commented
I am closing this issue since I misunderstood the code. It is correct.