Messaging Service Encode

In their tutorial, they encode the data in JSON before sending it out. Do we need to do this? Should we do this? What are the benefits? Downsides?

The purpose of the messaging service is to be able to send strings between servers. JSON strings are still strings, and Roblox wouldn’t have a reason to enforce the use of JSON on developers.

The benefit and downsides of doing this inherit the use in DataStores. Using JSON is a simple way to be able to support encoding and decoding of tables, but are unable to send tables with keys as non-consecutive integers (ex: {[2] = "Foo",[6] = "Bar"}).

So if I’m sending a table of data using PublishAsync, I should encode

The only reason you would need to is if encoding to JSON makes sense in your use case, which is sending tables between servers. If this isn’t a requirement, it is something that can be avoided.

1 Like

As @TheNexusAvenger said, you should try and avoid what I did in that tutorial but in some cases you need to send a table, if you wish to not use encode/decode then you can use string manipulation especially for a chat system but I wasn’t going to get in to that.

Encoding the data in JSON isn’t necessary for MessagingService and DataStore, they already automatically Encode and Decode data for you, so all you are doing is just double Encoding.

The answer is no

The answer is actually yes. PublishAsync accepts a string value, so it’s still necessary to encode your JSON data so you can send the string in the first place. You can’t send a raw table unless it’s coerced into a string similarly to DataStores (which, from my understanding, it’s not).

1 Like

https://developer.roblox.com/articles/Saving-Player-Data
This Roblox Official Article saids otherwise, they don’t use JSON Encode in their Example.

I never used JSON Encode for DataStoreService but to check the string length , same applies for MessageService, Bindable and Remote Objects.

Says otherwise for what?

You don’t have to. It’s coerced into a string on the backend and back into its original type when it’s fetched. It’s not explicitly a JSON format, though it does coercion. The same goes for a key and why you’re able to make keys just a player’s UserId.

Re-read my post.

It saids Variant

Check out my sample code it works without JSON Encode

2 Likes

Whooooooops, my bad. I must’ve breezed over it and associated string for topic with both fields. Thanks for the point-out. In this case, yeah, encoding it wouldn’t be necessary then.

So is universe scripts where we can make an effective server list ever coming out or do we just have to rely on an external service for now.

If I recall correctly, universe scripts were either delayed or cancelled. The roadmap currently lists them as postponed. If you’re up for making a dedicated server for server lists or anything, you can either attempt to use MessagingService to accomplish your needs or use an external service to serve as a dedicated server for retrieving server lists.