npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nakama-ts

v1.0.0

Published

JavaScript client for Nakama server written in TypeScript.

Downloads

50

Readme

Nakama JavaScript client

JavaScript client for Nakama server written in TypeScript. For browser and React Native projects.

Nakama is an open-source server designed to power modern games and apps. Features include user accounts, chat, social, matchmaker, realtime multiplayer, and much more.

This client implements the full API and socket options with the server. It's written in TypeScript with minimal dependencies to be compatible with all modern browsers and React Native.

Full documentation is online - https://heroiclabs.com/docs/javascript-client-guide

Getting Started

You'll need to setup the server and database before you can connect with the client. The simplest way is to use Docker but have a look at the server documentation for other options.

  1. Install and run the servers. Follow these instructions.

  2. Import the client into your project. It's available on NPM and can be also be added to a project with Bower or other package managers.

    yarn add "@heroiclabs/nakama-js"

    You'll now see the code in the "node_modules" folder and package listed in your "package.json".

  3. Use the connection credentials to build a client object.

    // <script src="path/to/nakama-js.umd.js"></script>
    var client = new nakamajs.Client("defaultkey", "127.0.0.1", 7350);
    client.ssl = false; // enable if server is run with an SSL certificate

Usage

The client object has many methods to execute various features in the server or open realtime socket connections with the server.

Authenticate

There's a variety of ways to authenticate with the server. Authentication can create a user if they don't already exist with those credentials. It's also easy to authenticate with a social profile from Google Play Games, Facebook, Game Center, etc.

var email = "[email protected]";
var password = "batsignal";
const session = await client.authenticateEmail({ email: email, password: password });
console.info(session);

Sessions

When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a Session object.

console.info(session.token); // raw JWT token
console.info(session.userId);
console.info(session.username);
console.info("Session has expired?", session.isexpired(Date.now() / 1000));
const expiresat = session.expires_at;
console.warn("Session will expire at", new Date(expiresat * 1000).toISOString());

It is recommended to store the auth token from the session and check at startup if it has expired. If the token has expired you must reauthenticate. The expiry time of the token can be changed as a setting in the server.

// Assume we've stored the auth token in browser Web Storage.
const authtoken = window.localStorage.getItem("nkauthtoken");
const session = nakamajs.Session.restore(authtoken);
if (session.isexpired(Date.now() / 1000)) {
    console.warn("Session has expired. Must reauthenticate.");
}

Requests

The client includes lots of builtin APIs for various features of the game server. These can be accessed with the methods which return Promise objects. It can also call custom logic as RPC functions on the server. These can also be executed with a socket object.

All requests are sent with a session object which authorizes the client.

const account = await client.getAccount(session);
console.info(account.user.id);
console.info(account.user.username);
console.info(account.wallet);

Socket

The client can create one or more sockets with the server. Each socket can have it's own event listeners registered for responses received from the server.

const secure = false; // enable if server is run with an SSL certificate
const trace = false;
const socket = client.createSocket(secure, trace);
socket.ondisconnect = (evt) => {
    console.info("Disconnected", evt);
};

const session = await socket.connect(session);
// Socket is open.

There's many messages for chat, realtime, status events, notifications, etc. which can be sent or received from the socket.

socket.onchannelmessage = (message) => {
    console.info("Message received from channel", message.channel_id);
    console.info("Received message", message);
};

const roomname = "mychannel";
const channel = await socket.send({ channel_join: {
    type: 1, // 1 = room, 2 = Direct Message, 3 = Group
    target: roomname,
    persistence: false,
    hidden: false
} });

const message = { "hello": "world" };
socket.send({ channel_message_send: {
    channel_id: channel.channel.id,
    content: message
} });

Contribute

The development roadmap is managed as GitHub issues and pull requests are welcome. If you're interested to enhance the code please open an issue to discuss the changes or drop in and discuss it in the community chat.

Source Builds

The codebase is written in TypeScript with tests in JavaScript and can be built with Rollup.js. All dependencies are managed with NPM.

yarn install && yarn build

Run Tests

To run tests you will need to run the server and database. Most tests are written as integration tests which execute against the server. A quick approach we use with our test workflow is to use the Docker compose file described in the documentation.

docker-compose -f ./docker-compose.yml up
yarn build && yarn test

Release Process

To release onto NPM if you have access to the "@heroiclabs" organization you can use Yarn.

yarn run build && yarn publish --access=public

License

This project is licensed under the Apache-2 License.