Page 1 of 2

A bunch of questions

Posted: 13 Aug 2025, 18:35
by PanagiotisMilios
Hello,

I've been looking at smartfox for creating a game but I have a few questions that ideally I would want to resolve before beginning with development.

First, I plan to use Mongodb for persistent storage. I assume it's feasible but I didn't find any guide for it, only for SQL databases.

Secondly, my main game model is complex and big (20-100 KB). Is it worth writing a huge serialization process breaking down every field into key/values or just a serialised json string is enough? The big reading process will only happen once on login and then the client will react to changes to the model he already has.

Also, my main extension will receive requests, put them in a blocking queue and I'll have a separate thread take them and process them (the game is slow paced, closely resembles turn based games). I'm gonna do this with implementing a runnable, I'm just wondering if SFs has something better in case I missed it. Also, any guides I could read on something like this will definitely help and/or any pitfalls I could perhaps avoid.

Also, i'm confused on whether i should use mmo room or plain rooms. My game is MMO but the players don't control just one character with an area of interest. They play on top of a board and control armies and cities so they have an area of interest but I'm not sure if that's the purpose here.

That's all that I can think of for now. Any help is much appreciated. Thank you!

Re: A bunch of questions

Posted: 14 Aug 2025, 06:40
by Lapo
Hello,
First, I plan to use Mongodb for persistent storage. I assume it's feasible but I didn't find any guide for it, only for SQL databases.

You can connect to any database. SmartFoxServer Extensions are just java code that responds to events and requests so, as long as MongoDB provides a way for a java program to connect to it, you will be able to use it in SFS2X.

We do have lots of SQL examples using JDBC but you can ignore them and use MongoClient instead, as explained here, for instance:
https://www.digitalocean.com/community/ ... e-tutorial

I've also noticed that a JDBC driver exists for Mongo: https://www.mongodb.com/docs/atlas/data ... c/connect/
but I am not sure if this works with your use case. After all JDBC is an interface designed for SQL databases.

Secondly, my main game model is complex and big (20-100 KB). Is it worth writing a huge serialization process breaking down every field into key/values or just a serialised json string is enough?

I don't think it's worth the pain to write a special serializer, if the data is only sent at login time. The JSON string will be compressed by the SFS2X protocol anyways.

Also, my main extension will receive requests, put them in a blocking queue and I'll have a separate thread take them and process them (the game is slow paced, closely resembles turn based games). I'm gonna do this with implementing a runnable, I'm just wondering if SFs has something better in case I missed it. Also, any guides I could read on something like this will definitely help and/or any pitfalls I could perhaps avoid.

I am not sure why you would want to do this. SmartFoxServer already takes care of enqueuing and processing the requests, so all you need to do is handling them and generate the relative responses (where needed).

Take a look at the SFS2X architecture, to get a better idea of how this works:
https://docs2x.smartfoxserver.com/Exten ... hreadModel

Also, i'm confused on whether i should use mmo room or plain rooms. My game is MMO but the players don't control just one character with an area of interest. They play on top of a board and control armies and cities so they have an area of interest but I'm not sure if that's the purpose here.

The reason for using an MMORoom vs a regular Room is optimizing the size of updates per second to all players. In other words it's one thing to send an update to 4-8 players, and it's another one to send the same update to 400 players.
If the game is not real-time it's likely you won't need an MMORoom, but there may be other factors to keep in consideration. For example the max number of players in a game Room.

If you're planning to handle hundreds or thousands of players then an MMORoom would be ideal because you can reduce the size of each broadcast when updating the game state.

Let us know if anything is unclear.
Cheers

Re: A bunch of questions

Posted: 14 Aug 2025, 08:18
by PanagiotisMilios
I am not sure why you would want to do this. SmartFoxServer already takes care of enqueuing and processing the requests, so all you need to do is handling them and generate the relative responses (where needed).


My game has a lot of task scheduling and there's a chance of read/write collisions between player actions and tasks on the same data and I want to ensure that there will be no failures.

I plan to use an optimistic update strategy on the client request and rollback in case of failure.

Also another question, my map is divided in hexes , each hex holds roughly 1 KB of data and there could be thousands of them 10k-100k. These are important to be sent over the network and updated in clients when something changes (i.e ownership).

I was considering storing them in a list of room variables. Is there a more efficient way of doing this? I have more state that I need to sync between clients, I'm just wondering if there are limitations to consider with room variables besides more bandwidth charges.

Edit: I'm a bit confused by this part. I found the java extensions cookbook and it shows a method for setting a particular room variable but that method doesn't exist in the docs. I assume the docs are up-to-date, not the cookbook.Nvm, I found the setVariable method in the Room api.

Re: A bunch of questions

Posted: 16 Aug 2025, 14:01
by Lapo
I was considering storing them in a list of room variables. Is there a more efficient way of doing this? I have more state that I need to sync between clients, I'm just wondering if there are limitations to consider with room variables besides more bandwidth charges.

Adding 10K...100K Room Variables to a Room can be expensive.
Room Variables are sent to each player when they join a Room, so every Join request could be pretty expensive. Also having thousands of variables than are often updated may overwhelm some clients and use lots of server resources.

I think you should build a prototype and see for yourself what is the impact on the server and client side.
Cheers

Re: A bunch of questions

Posted: 18 Aug 2025, 09:15
by PanagiotisMilios
Keep in mind that the rate of updates per variable are slow and the game is persistent (Although I forgot to mention that). When I say slow, I mean once-twice per day slow and in late-game possibly less. We're also hosting in bare metal that comes with a lot more forgiving bandwidth charges and dedicated resources.

My questions are mostly whether smartfox has limitations around room variables. The 100k was a bit exaggerating but it's still the end goal. That being said, the initial tests will start with 3k-5k room variables and 500 player limit. Like you said there's the problem at player joining a room as that might cause a hiccup.

Re: A bunch of questions

Posted: 19 Aug 2025, 06:36
by Lapo
PanagiotisMilios wrote:Keep in mind that the rate of updates per variable are slow and the game is persistent (Although I forgot to mention that). When I say slow, I mean once-twice per day slow and in late-game possibly less.

I see. It certainly makes a lot of difference.
We're also hosting in bare metal that comes with a lot more forgiving bandwidth charges and dedicated resources.

Good choice.

My questions are mostly whether smartfox has limitations around room variables.

There is no particular limit, besides the hardware being able to keep up with how many Rooms and Users there will be.
The 100k was a bit exaggerating but it's still the end goal. That being said, the initial tests will start with 3k-5k room variables and 500 player limit. Like you said there's the problem at player joining a room as that might cause a hiccup.

You can test the scenario on a local machine or test server and see how it behaves.
Here you can find an article on how to build a stress test: https://smartfoxserver.com/blog/buildin ... test-tool/

We can also provide a 30 days unlimited license for larger tests if necessary. You can request it by sending an email to our support@... email.

Cheers

Re: A bunch of questions

Posted: 19 Aug 2025, 19:30
by PanagiotisMilios
Thanks. Will do.

On the subject of room variables, could you clarify one thing please.

I read that it's wrong using the setVariable() method of the Room class and it won't broadcast the changes to users.

So the correct way would be getParentRoom().get variables() then changing the variables I want from the list and then reassign using the getApi().setRoomVariables() method?

Re: A bunch of questions

Posted: 20 Aug 2025, 06:27
by Lapo
No, just use getApi().setRoomVariables(...) and pass a list of RVs.
Cheers

Re: A bunch of questions

Posted: 21 Aug 2025, 06:30
by Lapo
This is the part that confuses me. If I have already 100 RVs and set the method with a list of 50 where 25 share the same name and values and 25 don't, what will SFS do?

Variables with the same names update the value of existing ones and the new ones are added to the list of Room Variables.
As regards updates towards clients only values that changed from their previous state will be transmitted.

Cheers
p.s. = sorry your last message got deleted by mistake, but the question is quoted in my reply here.
If there's anything else let us know

Re: A bunch of questions

Posted: 21 Aug 2025, 09:12
by PanagiotisMilios
Great thanks a lot. That's all for now, I think I have an idea on how to proceed.

Re: A bunch of questions

Posted: 05 Sep 2025, 19:40
by PanagiotisMilios
Another bump here. I need to make an http request to an external service during the user login. The external service determines if the login succeeds or not. I don't want to write blocking code but i'm not sure if there's another way to do it. I come from c# background and i have some difficulty understanding java's equivalent. In c# i would simply await the request.

In java is this the close equivalent?

Code: Select all

var request = HttpRequest.newBuilder(URI.create("https://example.com")).build();

HttpResponse<String> response = client
                .sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .join();

Re: A bunch of questions

Posted: 06 Sep 2025, 15:42
by Lapo
You should write blocking code in this case because the login process needs to run in the same thread that started it.
It should be of no concern because Extension threads are pooled and if they become all busy the pool will auto-resize itself. However it's always recommendable to keep the latency of the remote service as low as possible.

Cheers

Re: A bunch of questions

Posted: 06 Sep 2025, 17:19
by PanagiotisMilios
Great! Thanks a lot

Re: A bunch of questions

Posted: 29 Sep 2025, 17:11
by PanagiotisMilios
Another bump here.

I have a task that i need to run periodically but only schedule it as soon as the previous task has finished running (Something like scheduleWithFixedDelay).

Is it ok to do it like this? Could this lead to issues?


Code: Select all

public class MyExtension extends SFSExtension {
   
   @Override
   public void init() {
      SmartFoxServer sfs = SmartFoxServer.getInstance();
          sfs.getTaskScheduler().schedule(new TaskRunner(), 50, TimeUnit.MILLISECONDS);
   }   

   private class TaskRunner implements Runnable
      {
           SmartFoxServer sfs;
           public void run()
           {   
         //Do stuff
         sfs.getTaskScheduler().schedule(this, 50, TimeUnit.MILLISECONDS);
      }
   }
}

Re: A bunch of questions

Posted: 30 Sep 2025, 06:32
by Lapo
Hi,
no that's fine, it is common to use this mechanism for tasks.

Cheers