Help with this use-case

Hi there,

I’m currently working on a “little” project. I won’t go into detail about what it is exactly, but I’ll explain the use case. The website provides a way to protect the source of Lua code for games. It does this by running the code in a sandboxed environment on a VPS (we can’t do it on a worker because of the memory limit) and then, when the code needs to interact with the game, you send little snippets of more Lua code for execution on the game.

Now, here’s how the system works. First, when the game starts, it sends a request to our Cloudflare Worker that takes the cf-connecting-ip header to verify the request is really from the game server. The worker then tells the VPS to start executing the code. Now, let me give you a little pseudo-code snippet of what the Lua code might be.

-- Listens for when a part in the game is touched
local functions =  marketplace.functions()
local part = functions.getObject('workspace.Part')

functions.registerEvent('Touched', part, function()
    print('Part touched')
end)

Now, this code doesn’t look like much, but behind the scenes, it’s doing a lot. First, the getObject method actually sends a request to the game server telling it to return that part. Then, registerEvent tells the game server to send a request to the worker everytime that part is touched.


Now that I have the use case explained, let me explain the issue. The game sends the initial request telling the server it has started, which then tells the VPS to start executing the code. The VPS might then tell the game to listen for a part to be touched and then the game sends a request back to the worker when the part was touched which then has to tell the VPS that the part was touched to execute the callback.

There’s a lot going on there, but I need to find the most efficient way for all of those different requests to communicate with each other. I want that first initial request to create some sort of function that runs until the game ends which all the other requests feed into so it can handle everything for that game in one central place.

Not sure if this made any sense, I can try and explain it some more if you need me to.

Hey @grilme99! Welcome :slight_smile:

I want that first initial request to create some sort of function that runs until the game ends

Due to the execution limits for a Worker script, you would be unable to start a function and run it continuously during a game. That being said, if you’re trying to do this in a serverless context (e.g. if you try and build this on a different serverless provider), long-running requests are going to get expensive - is it possible to track state for this in something like Workers KV?

2 Likes

Hey! Thank you for your reply.

I took a look at Workers KV before but decided it wouldn’t be the right choice because, while it has fast read speeds, it says that write speeds are far slower. I would require something that is extremely fast both when reading and writing for tracking state with my use case.

For example, let’s go back to the part touched example in the OP. The game would send a request to a worker, which would then update something in a database to say that the part has been touched. I would then need that original request to pickup that database change as quickly as possible so there is very little latency. Do you know if there is a better option? I’m not on the biggest of budgets, either.

The problem with a game is the gazillion of requests going back and forth, you’d be bankrupt before you’d even get started. I’d suggest looking at a full framework like https://feathersjs.com/ to get started quickly and iron out a back-end.

How would a framework like Feathers help me? I could use a VPS for the requests that the game sends?

I still need to find the best option for a database.

My point was only that servless alternatives would be too expensive, just like signalnerve pointed out as well. The FeatherJS framework can do the same thing as any webworker can do and a lot more, and it makes handling websockets easier and can keep a connection open just as you wanted. It also supports most databases out there: https://docs.feathersjs.com/api/databases/adapters.html