heroiclabs / nakama-js

JavaScript client for Nakama server written in TypeScript.
https://heroiclabs.com/docs/nakama/client-libraries/javascript/
Apache License 2.0
198 stars 55 forks source link

How to enable nodejs support #23

Open nikitaosyak opened 6 years ago

nikitaosyak commented 6 years ago
ind-igo commented 5 years ago

Official support would be great.

dongshimou commented 4 years ago

@nikitaosyak thanks. npm install all packages. then it's work fine. but when i use

client.authenticateEmail

thow an err err: ReferenceError: btoa is not defined at Client.authenticateEmail (/Users/xxx/Desktop/nakama-js/node_modules/@heroiclabs/nakama-js/dist/nakama-js.cjs.js:3765:13)

i try to npm install --save buffer and npm install --save btoa-atob,but invalid.

louis030195 commented 4 years ago

Any update on this ?

Current hack to make it work:

npm i es6-promise isomorphic-fetch url-search-params ws btoa atob
sed -i "2i require('es6-promise').polyfill()\nrequire('isomorphic-fetch')\nvar btoa = require('btoa')\nvar atob = require('atob')\nconst URLSearchParams = require('url-search-params')\nvar WebSocket = require('ws')\nvar self = {}" node_modules/@heroiclabs/nakama-js/dist/nakama-js.cjs.js
novabyte commented 4 years ago

@louis030195 At the moment we don't have plans to provide compatibility with Node. The current client uses its dependency tree carefully to ensure it will load in Google Instant, Snap Games, and Facebook Instant Games webview environments on mobile browsers.

Would be happy to look at a pull request which does not break these environments at build or package time and introduces Node support if you have the time?

adbourdages commented 2 years ago

And old thread but it got me on the right track to add nakamajs into a command line node project.

Here is the updated code to add to the top of najama-js.cjs.js

require('es6-promise').polyfill();
require('isomorphic-fetch');
const URLSearchParams = require('url-search-params')
var WebSocket = require('ws')
var XMLHttpRequest = require("xhr2")
var self = {} // dont forget this also, in node this object will not be created

And here is an example of how to setup your calls to the client

import pkg from "@heroiclabs/nakama-js";
const {Client} = pkg;

let client = new Client("changethis", "127.0.0.1", 7350);

async function run()
{
    try
    {
        let session = await client.authenticateDevice("<deviceId>", true, "importer");

            try {
                    var payload = { "item": "cowboy"};
                    var response = await client.rpc(session, "EquipHat", payload);
                    console.log("New hat equipped successfully", response);
            }
            catch (error) {
                    console.log("Error: %o", error);
            }

        console.log("done");
    }
    catch (err)
    {
        console.log("Error authenticating device: %o:%o", err.statusCode, err.message);
        console.log("Error authenticating device: %o", err);
    }
}

run();

Finally the package.json file

{
  "name": "nakama-import-3",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@heroiclabs/nakama-js": "^2.3.0",
    "xhr2": "^0.2.1",
    "node-fetch": "^3.2.1",
    "es6-promise": "^4.2.8",
    "isomorphic-fetch": "^3.0.0",
    "url-search-params": "^1.1.0",
    "ws": "^8.5.0"
  },
  "type": "module"
}