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:
Is the JSON payload simply too big? If yes, how do go about constraining the payload?
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
.