Importing NakamaJs module


#1

My Code:

var ItsAscript = pc.createScript('itsAscript');


ItsAscript.prototype.initialize = function() {
    
    var nakamajs = document.createElement('script');
    nakamajs.src = '/nakama-js.umd.js';
    document.head.appendChild(nakamajs);

    //window.alert("OKA");
    
    var client = new nakamajs.Client("defaultkey", "192.168.1.44", 7350);
    client.ssl = false;
    print("print ho rha hai");
    
    var email = "super@heroes.com";
    var password = "batsignal";
    var session = client.authenticateEmail({ email: email, password: password });
    console.info(session);
    
};

// update code called every frame
ItsAscript.prototype.update = function(dt) {
    
};


//const client = new nakamajs.Client("defaultkey");

I’m trying to import NakamaJs module but I keep getting this error:

[ItsAScript.js?id=16996646&branchId=5a76c294-f453-41ae-be63-d7b54f2861dd:15]: nakamajs.Client is not a constructor

TypeError: nakamajs.Client is not a constructor


#2

I’m also trying to use Nakama with PlayCanvas, if anyone has any suggestions that would be welcome.

I’ve just put nakama-js.umd.js in the scripts folder, and then in my other .js script in the initialize() function, I use OP’s code to import Nakama (just the 3 lines of code in his post), but the Chrome debug log shows:
Failed to load resource: the server responded with a status of 404 (Not Found)

EDIT: Nakama’s documentaion (https://heroiclabs.com/docs/javascript-client-guide/) explains that the only requirement is to include nakama-js.umd.js in the script html tag. So I’m about to search the forums for how to do that.


#3

If you have nakama-js.umd.js in the project, then it’s automatically added to the header. Just make sure that it is first in the script loading order in the project settings.


#4

Thank you @yaustar. This has worked for me.