1

Sometimes I am getting the error 'Unhandled rejection Error: Integer is unsafe' when sending JSON data via socket.io 2.0. Here is an example of sending a tweet object that results in such an error.

Example Code: https://gist.github.com/whoisstan/dcba1471094b984514c436fd395364e2

I am using those packages on node 6.11.2:

  • "redis": "^2.8.0",
  • "socket.io": "~2.0",
  • "socket.io-redis": "^5.1.0"

Is the JSON payload simply too big? If yes, how do go about constraining the payload?

Stan Wiechers
  • 1,962
  • 27
  • 45
  • Code required to understand and answer the question MUST be pasted into your actual question, not only available via a link. This is because links have a habit of changing or disappearing over time rendering the question useless as a long term reference. Please add the relevant portions of the code directly to your question and then format them appropriately. – jfriend00 Aug 22 '17 at 00:09
  • What is the source of this JSON? Where does it come from? – jfriend00 Aug 22 '17 at 00:14
  • it's straight from the twitter api, that's why i was confused that it doesn't play well as a socketio payload. – Stan Wiechers Aug 22 '17 at 20:21
  • Twitter itself explains the issue in the development document I linked in my answer. Are you parsing this in node.js? In what version? – jfriend00 Aug 22 '17 at 20:26
  • I am a retrieving the data from twitter, putting an envelope around it and put it in mongodb, queue. No encoding issue along that process. Only when sending it via socket2. – Stan Wiechers Aug 22 '17 at 21:43

1 Answers1

1

Max safe integer in Javasvcript is:

9007199254740991

Your JSON contains integers like:

899068272867328000

which exceeds the limit.

Things which do not need to be actually used as numbers (one of these large numbers is labelled as an "id" could perhaps just be used as strings instead of numbers so put them into the JSON as strings, not numbers.

If this data is from Twitter, here's a discussion of the issue in Twitter's development doc: Twitter IDs. Note, the JSON structure offers id_str as an alternative which is indeed a string.

And, per that article, you will either need to preprocess the JSON before parsing it to remove the id values or you will need to get a parser that does not throw an exception with these long ids, but rather just truncates them or converts them to null or NaN. And, in either case your code needs to use .id_str, not .id.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Good find and good answer. The value is retrieved from a twitter API payload, parsed and saved into mongo, queued and then retrieved for sending it via socket, only on this last leg the error appears. At the end of the day i like to avoid having to pre-parse and adjust all the data that we are retrieving and not break the enconding. – Stan Wiechers Aug 22 '17 at 21:49