question

Lucilo Del Castillo avatar image
Lucilo Del Castillo asked

Using PlayFab with React ?

I am struggling trying to implement PlayFab SDK on my React App, i am quite beginner on React, and cant find a well explained documentation to incorporate it in my react project.

* i created a new react app using -npx create-react-app playfabtest

* then installed playfab sdk for node: -npm install playfab-sdk

* copied to my App.js :


tried also using '=require(' like the commented line with no success

the error i am getting is this:


So, there is a way to fix this and use Playfab from my react app ?
Thanks !!!

sdks
10 |1200

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

Lucilo Del Castillo avatar image
Lucilo Del Castillo answered

I could make it working moving the files PlayFabClient.js and PlayFab.js from PlayFab SDK installed in node_modules to inside the /src folder of my react app. Doing that, the "import "Playfab from './(Folder where the files are)" worked perfectly and the example functions in the PlayFab docs works as expected.
i even needed to setup an express server, since i am doind the API calls straigh away from the react app.

1 comment
10 |1200

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

Made Wang avatar image Made Wang commented ·

Thanks for sharing.

0 Likes 0 ·
Lucilo Del Castillo avatar image
Lucilo Del Castillo answered

Sorry, my images didnt uplodad...


my App.js:

import logo from './logo.svg';

import './App.css';

import { PlayFabClient } from 'playfab-sdk';


// var PlayFabClient = require('./PlayFabSdk/Scripts/PlayFab/PlayFabClient.js')
function App() {

PlayFabClient.settings.titleId = ""; PlayFabClient.settings.developerSecretKey = "";
PlayFabClient.GetTitleData({Keys : ["Sample"]}, function(error, result) { if(error) { console.log("Got an error: ",error); return; }
console.log("Reply: ",result); });

return (

<div className="App">

<header className="App-header">

<img src={logo} className="App-logo" alt="logo" />

<p> Edit <code>src/App.js</code> and save to reload.</p>

<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" > Playfab Test </a> </header> </div> ); }

--------------------------------------------------

and the error i get is :

Compiled with problems:X

ERROR in ./node_modules/playfab-sdk/Scripts/PlayFab/PlayFab.js 2:10-24

Module not found: Error: Can't resolve 'url' in 'D:\Github\playfabtest\node_modules\playfab-sdk\Scripts\PlayFab' BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it. If you want to include a polyfill, you need to: - add a fallback 'resolve.fallback: { "url": require.resolve("url/") }' - install 'url' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "url": false }

10 |1200

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

Made Wang avatar image
Made Wang answered

You should install a Js PlayFabSDK, but you installed a NodeJs PlayFabSDK, you can refer to JavaScript SDK - PlayFab | Microsoft Docs and use npm install playfab-web-sdk to install the Js PlayFabSDK.

Secondly, referring to Do you have a SDK/wrapper for React Native? - Playfab Community, if the Js PlayFabSDK does not work in your React project, you can try to generate an SDK yourself.

Also, if you want to call GetTitleData, you need to log in to PlayFab first, for example by calling LoginWithCustomID, refer to JavaScript quickstart for Native and Phaser - PlayFab | Microsoft Docs.

10 |1200

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

Lucilo Del Castillo avatar image
Lucilo Del Castillo answered

Thanks you very much for your answers Made Wang,

sorry for bother if may this are quite beginner questions...

So i am trying to implement Js PlayFabSDK for JavaScript on a React Project created with npx create-react-app

but i am having problems to wrap the Apis to the functions, in my App.js file

i tried something like this :

import logo from './logo.svg';
import './App.css';
// import {PlayFabClientApi as PlayFab} from './PlayFabClientApi.js'
// import { PlayFab } from 'playfab-web-sdk';
// import { PlayFab } from 'playfab-web-sdk/PlayFabClientApi';
 //  var PlayFabClientApi = require('./PlayFabClientApi.js')  function App(){



    PlayFab.settings.titleId = document.getElementById("titleId").value;
    var loginRequest = {
        // Currently, you need to look up the required and optional keys for this object in the API reference for LoginWithCustomID. See the Request Headers and Request Body.
        TitleId: PlayFab.settings.titleId,
        CustomId: document.getElementById("customId").value,
        CreateAccount: true
    };
    PlayFabClientSDK.LoginWithCustomID(loginRequest, LoginCallback);
}
// callback functions take two parameters: result and error
// see callback functions in JavaScript if unclear
var LoginCallback = function (result, error) {
    if (result !== null) {
        document.getElementById("resultOutput").innerHTML = "Congratulations, you made your first successful API call!";
    } else if (error !== null) {
        document.getElementById("resultOutput").innerHTML =
            "Something went wrong with your first API call.\n" +
            "Here's some debug information:\n" +
            PlayFab.GenerateErrorReport(error);
    }
  return (
    <div className="App">
      <header className="App-header"/>
      
    PlayFab Getting Started Guide
    TitleID: <input type="text" id="titleId" value="144"/>
    CustomID: <input type="text" id="customId" value="GettingStartedGuide"/>
    <input type="button" value="Call LoginWithCustomID" onclick="DoExampleLoginWithCustomID()" />
    
    <textarea id="resultOutput" cols="60" rows="5"></textarea>
      
    </div>
  );
  };
export default App;

And i get error like this:

Compiled with problems:XERROR in ./src/App.js 8:0-59Module not found: Error: Can't resolve 'playfab-web-sdk/PlayFabClientApi' in 'D:\Github\playfabtest\src'
2 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.

Made Wang avatar image Made Wang commented ·

To clarify, as said in the thread I mentioned above, the PlayFabWebSDK is not packaged for React, so we can't guarantee it will work in a React project. But you can try to regenerate an SDK using the SDK Generator, or make a feature request for it.

Judging from the error message, the program does not find the SDK, it should be a path problem, I am not familiar with React, does it support the import method of '<script></script>'?

0 Likes 0 ·
Lucilo Del Castillo avatar image Lucilo Del Castillo Made Wang commented ·

Usually, i use ' import {ThirdPartyService} from ' ThirdPartyServices; ', to importing
but seems it don't work straight away with React, i believe should be some easiest way to make it work, without going trough making a SDK with SDK Generator.

I will keep searching,
Thanks a lot

0 Likes 0 ·
Lucilo Del Castillo avatar image
Lucilo Del Castillo answered

I am on the way on figuring out how to do the React Setup for using Playfab SDK.

Following other tutorials on how to connect a react app with a database, is creating a server.js file on the main folder of the project,

Then, it use npm install express and run it on a different port as a server, that makes the api calls to the data base and send to the front end.

doing this, i could install PlayFab SDK and makes API calls to my Playfab Title.

Wonderfull !

but now i am struggling to use those functions trough the express functions,
like for example on the tutorials with mongoDB, it calls the API this this function:

const express = require('express'); 

var PlayFab = require("./node_modules/playfab-sdk/Scripts/PlayFab/PlayFab");
var PlayFabClient = require("./node_modules/playfab-sdk/Scripts/PlayFab/PlayFabClient");
const app = express();
const cors = require('cors');app.get('/login' , function (req, res) {
    res.send('Éxpress is here')
})

......

So i still cannot figure out how to write the functions for Playfab like this one:

function DoExampleLoginWithCustomID() {
    PlayFab.settings.titleId = "MyTitleId";
    var loginRequest = {
        // Currently, you need to look up the correct format for this object in the API reference for LoginWithCustomID. The Request Headers and Request Body are included as keys and values in the request object.
        TitleId: PlayFab.settings.titleId,
        CustomId: "GettingStartedGuide",
        CreateAccount: true
    };
    // For functions in the Node SDK, the first parameter will be the request object and the second parameter will be the callback function. The callback function executes after the request returns.
    PlayFabClient.LoginWithCustomID(loginRequest, LoginCallback);
} 




Please anyone can tell me if its a proper way to do this, and if its, how to use the api calls functions using the express app.post, app.get functions ?.

Thanks a lot
1 comment
10 |1200

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

Made Wang avatar image Made Wang commented ·

I'm also very new to express and can't give a sample. It is recommended that you refer to the official documentation of express to understand how to write the method.

0 Likes 0 ·

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.