question

plosnita avatar image
plosnita asked

Node SDK for local CloudScript development

I'm trying to develop an application and create some sort of local test environment using Node SDK

Server or Client calls actually wrap around MakeRequest which is async and needs a handler function.

So, for example:


var playerInventoryResult = server.GetUserInventory({ PlayFabId: currentPlayerId });

has two outcomes depending where I run it.

1. On CloudScript playerInventoryResult will become the inventory of the player and I can use it right away

2. On my local JS file playerInventoryResult will be undefined

Is there a way to have an unified behaviour? My expectation was that the Node SDK will emulate the cloudscript behaviour.

My aim is to write and test locally and then just upload (i.e. server.GetUserInventory to behave the same for example)

sdksCloudScripttest
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
JayZuo avatar image
JayZuo answered

Node.js SDK won't emulate the CloudScript behaviour. It's just a wrapper around PlayFab's RESTful API which makes it easier to use in Node.js and not aimed for CloudScript development. For example, in CloudScript, you can use currentPlayerId which is the PlayFab Id of the current player that triggered the Cloud Script call. However, this is not is not existed in Node.js SDK. And in CloudScript, you can use server to access all server-side API calls with no other steps, but with Node.js SDK, you will need to set developerSecretKey before calling Server APIs.

So to make your code work locally, you will need to add some additional code. Besides, for writing CloudScript, I'd suggest using TypeScript. And for testing purpose, this question: https://community.playfab.com/questions/3854/recommendations-for-unit-testing-cloud-script.html might help you.

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

plosnita avatar image plosnita commented ·

Hi,
Thanks for your quick response. I've seen that thread, but does not provide the information I was looking for. It's a two year old response (surely things got better by now) of some work in progress with caveats:

Be careful not to make too many api calls too quickly, or you'll get throttled. We will eventually have better options for developing and debugging Cloud Script, but for now do your best to test it however you can.

My question was not about unit testing, especially not unit testing the playfab sdk (that is not my job)

What I want is to be able to develop CloudScript functions locally, run them on my environment, debug, fix, optimize and then upload to PlayFab.

Maybe there is a better option than the Node SDK, that was just an assumption of mine that it would be the way to go about it.

For me as a developer it's counter-intuitive that asynchronous calls will magically behave synchronously when run by PlayFab.

I've got all the other things needed like the currentPlayerId, developerSecretKey, that is not the problem. The problem is that the code will look differently than on CloudScript. I need to have another data source for GetUserInventory (for example).

Thank you

0 Likes 0 ·
JayZuo avatar image JayZuo ♦ plosnita commented ·

Cloud Script is a set of Javascript functions compiled with V8 and hosted on PlayFab's servers. And unlike Node.js SDK or JavaScript SDK, server-side API calls are called synchronously, like so: var result = server.AuthenticateUserTicket(request);. If there is no issue, you will get the result. And any error in the API call will causes an exception to throw. There will be no success or error callback. I'm afraid there is no better option here. What I would suggest here is using TypeScript for development. And if there is any API or code you are not familiar, you can test with Node.js SDK locally first and then convert it into CloudScript (although the code will look differently than on CloudScript, but the logic and most parts are same). Then you will need to upload your CloudScript for testing. Besides, you are also welcome to Post an idea for your feature request.

0 Likes 0 ·
plosnita avatar image plosnita JayZuo ♦ commented ·

Thank you for clearing that up. Just some quick follow up questions:

If I do a call to another external service from CloudScript, will it be synchronously?

Can I get data from the PlayFab CDN in a CloudScript function? Is this something I should avoid?

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.