MessagingService's "size of message" limit

Hello, I want to get the accurate size of data.

local HttpService = game:GetService("HttpService")
local function getDataSize(data)
	return #HttpService:JSONEncode(data)
end

This function is according to MessagingService not correct.


local t = {}
for i = 1, 71 do
	table.insert(t, 1111111111111)
end

print(getDataSize(t)) --prints under 1000, 1kB is 1024

game:GetService("MessagingService"):PublishAsync("Test", t)

MessagingService says that the data exceeded 1kb.
Any help is appreciated!

What do you mean with that? :thinking:

The # operator gets the size of the table in terms of how many elements it has, not how much memory it takes up.

Although I suppose you could create a function to get the sizeof a table in memory, though you would have to count for a lot of cases. Though here is how you do it if you only had an array of numbers as in your second example

-- Lua uses 64 bit integers and 64 bit double precision floats

function sizeof(t)
 return #t * 64
end

1 Like

JSONEncode converts a table into a JSON string. That means that the # operator returns how long the string is, not how many elements in the table are.

1 Like

What are you trying to solve by figuring out the exact max payload?

1 Like

I’m trying to make a packet switching system where data gets split into packets when it’s bigger than 1kB.
Is the “size of message” limit of 1kB maybe calculated including the Data/Sent fields?

This would explain why 976B is the maximum size:

MessagingService:PublishAsync("Test", string.rep("a", 974)) --has a size of 976
MessagingService:PublishAsync("Test", string.rep("a", 975)) --too large
1 Like

Yes, that sounds accurate. At one point we looked at increasing it so the data alone could be 1kB but we never prioritized that fix.

1 Like

Ah okay, thanks for your help! :slightly_smiling_face:

1 Like