Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enable nodejs support #23

Open
nikitaosyak opened this issue Apr 17, 2018 · 5 comments
Open

How to enable nodejs support #23

nikitaosyak opened this issue Apr 17, 2018 · 5 comments

Comments

@nikitaosyak
Copy link
Contributor

  • Take cjs release
  • Install following packages: es6-promise, isomorphic-fetch, url-search-params, ws
  • Monkey patch the cjs release (on top of the file):
// nakama-js.cjs.js
require('es6-promise').polyfill();
require('isomorphic-fetch');
const URLSearchParams = require('url-search-params')
var WebSocket = require('ws')
var self = {} // dont forget this also, in node this object will not be created
  • Use it like in examples, just don't forget to require it
// app.js
const nakamajs = require('/.nakama-js.cjs')
const client = new nakamajs.Client('defaultkey', '127.0.0.1')
@ind-igo
Copy link

ind-igo commented Oct 22, 2019

Official support would be great.

@dongshimou
Copy link

dongshimou commented Apr 8, 2020

@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
Copy link

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
Copy link
Member

novabyte commented Aug 9, 2020

@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
Copy link

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants