Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "java.io.EOFException: JSON error" using the clojure twitter-api

I've written some simple clojure code that accesses the twitter streaming api. My code is essentially the same as the example code described in the twitter-api docs:

(def ^:dynamic *custom-streaming-callback* 
  (AsyncStreamingCallback. (comp println #(:text %) json/read-json #(str %2)) 
                           (comp println response-return-everything)
                           exception-print))

(defn start-filtering []
  (statuses-filter :params {:follow 12345}
                   :oauth-creds *creds*
                   :callbacks *custom-streaming-callback*))  

I'm following tweets about a specific user and using oauth for authentication (not shown). When I run the start-filtering method and a connection is opened with twitter everything works well for a spell, but if the stream is inactive for a bit (around 30 seconds), i.e. no tweets about this particular user are coming down the pike, the following error occurs:

#<EOFException java.io.EOFException: JSON error (end-of-file)>

I assumed from the twitter docs that when using a streaming connection, twitter keeps the stream open indefinitely. I must be making some incorrect assumptions. I'm currently diving into the clojure twitter-api code to see what's going on, but I thought more eyes would help me figure this out more quickly.

like image 459
mofeeta Avatar asked Apr 11 '12 22:04

mofeeta


1 Answers

I had the same issue that you have. As you found, the streaming function emits an empty message if no data has been received in the last thirty seconds or so. Trying to read this as json then causes the EOF exception that you see.

I don't know of any way to prevent these calls. In my case I worked around the issue with a simple conditional that falls back to an empty map when there is no JSON to read.

(if-not (clojure.string/blank? %)
   (json/read-str % :key-fn keyword)
   {})
like image 160
Dan Midwood Avatar answered Nov 15 '22 08:11

Dan Midwood