All Projects → heroiclabs → Nakama Js

heroiclabs / Nakama Js

Licence: apache-2.0
JavaScript client for Nakama server written in TypeScript.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Nakama Js

Nakama
Distributed server for social and realtime games and apps.
Stars: ✭ 5,293 (+5140.59%)
Mutual labels:  game-server, realtime
Realtime Server
A realtime dedicated game server ( FPS / MOBA ). 一个实时的专用游戏服务器.
Stars: ✭ 358 (+254.46%)
Mutual labels:  game-server, realtime
nakama-java
Android optimized Java client for Nakama server.
Stars: ✭ 26 (-74.26%)
Mutual labels:  realtime, game-server
Rnl
RNL - Realtime Network Library - The opensource reliable UDP network library
Stars: ✭ 59 (-41.58%)
Mutual labels:  game-server, realtime
Azerothcore Wotlk
Complete Open Source and Modular solution for MMO
Stars: ✭ 1,330 (+1216.83%)
Mutual labels:  game-server
Simple Realtime Message Socketio Nodejs Ci
🔌 Simple realtime push message using Socket.IO (Node.JS) integrated with PHP (CodeIgniter) and database MySQL
Stars: ✭ 85 (-15.84%)
Mutual labels:  realtime
Aurio
Audio Fingerprinting & Retrieval for .NET
Stars: ✭ 84 (-16.83%)
Mutual labels:  realtime
Lockstepplatform
Stars: ✭ 84 (-16.83%)
Mutual labels:  game-server
Maskyolo caffe
YOLO V2 & V3 , YOLO Combined with RCNN and MaskRCNN
Stars: ✭ 101 (+0%)
Mutual labels:  realtime
Sandstone
PHP microframework designed to build a RestApi working together with a websocket server. Build a real time RestApi!
Stars: ✭ 98 (-2.97%)
Mutual labels:  realtime
Nodefony Starter
Nodefony Starter Node.js Framework
Stars: ✭ 95 (-5.94%)
Mutual labels:  realtime
Thunder
⚡️ A Go framework for rapidly building powerful graphql services
Stars: ✭ 1,281 (+1168.32%)
Mutual labels:  realtime
Awesome Voxel
Voxel resources for coders
Stars: ✭ 96 (-4.95%)
Mutual labels:  realtime
Arangochair
🥑 arangochair is a Node.js module that adds changefeed capability to ArangoDB and make it realtime push ready
Stars: ✭ 85 (-15.84%)
Mutual labels:  realtime
Rtmlton
MLton with Realtime GC and Threading features
Stars: ✭ 98 (-2.97%)
Mutual labels:  realtime
Codeigniter Ratchet Websocket
This library contains the demo of commenting/posting realtime using CodeIgniter+AngularJS+Ratchet PHP Websocket
Stars: ✭ 84 (-16.83%)
Mutual labels:  realtime
Endurox
Enduro/X Middleware Platform for Distributed Transaction Processing
Stars: ✭ 91 (-9.9%)
Mutual labels:  realtime
Aaronvandenberg.nl
⚛️ Web Developers portfolio build with Gatsby.js & React.js
Stars: ✭ 98 (-2.97%)
Mutual labels:  realtime
Gdb
Generic Data Binder (GDB) for jQuery is a framework agnostic and extremely easy to use 2 way data binder. GDB binds views and models in realtime with live two-way binding and no hefty framework necessary.
Stars: ✭ 90 (-10.89%)
Mutual labels:  realtime
Realtime Server Ue4 Demo
A UE4 State Synchronization demo for realtime-server. 为realtime-server而写的一个UE4状态同步demo.
Stars: ✭ 88 (-12.87%)
Mutual labels:  realtime

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

    Optionally, if you would like to use the Protocol Buffers wire format with your sockets, you can import the adapter found in this package:

    yarn add "@heroiclabs/nakama-js-protobuf"
    
  3. Use the connection credentials to build a client object.

    import {Client} from "@heroiclabs/nakama-js";
    
    var useSSL = false; // Enable if server is run with an SSL certificate.
    var client = new Client("defaultkey", "127.0.0.1", "7350", useSSL);
    

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, 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.

If you are using the optional protocol buffer adapter, pass the adapter to the Socket object during construction:

import {WebSocketAdapterPb} from "@heroiclabs/nakama-js-protobuf"

const secure = false; // Enable if server is run with an SSL certificate
const trace = false;
const socket = client.createSocket(secure, trace, new WebSocketAdapterPb());

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);
};


// 1 = room, 2 = Direct Message, 3 = Group
const type : number = 1;
const roomname = "mychannel";
const persistence : boolean = false;
const hidden : boolean = false;

const channel = await socket.joinChat(type, roomname, persistence, hidden);

const message = { "hello": "world" };
socket.writeChatMessage(channel.channel.id, message);

Contribute

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

Source Builds

Ensure you are using Node v12.18.1.

The codebase is multi-package monorepo written in TypeScript and can be built with esbuild. All dependencies are managed with Yarn.

To build from source, install dependencies and build the nakama-js and nakama-js-protobuf subrepositories:

yarn workspace @heroiclabs/nakama-js install && yarn workspace @heroiclabs/nakama-js build
yarn workspace @heroiclabs/nakama-js-protobuf install && yarn workspace @heroiclabs/nakama-js-protobuf 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.

Tests are run against each workspace bundle; if you have made source code changes, you should yarn workspace <workspace> build prior to running tests.

docker-compose -f ./docker-compose.yml up
yarn workspace @heroiclabs/nakama-js-test run test

Protocol Buffer Web Socket Adapter

To update the generated Typescript required for using the protocol buffer adapter, run the following:

npx protoc \
--plugin="./node_modules/.bin/protoc-gen-ts_proto" \
--proto_path=$GOPATH/src \
--ts_proto_out=packages/nakama-js-protobuf \
--ts_proto_opt=snakeToCamel=false \
--ts_proto_opt=useOptionals=true \
--ts_proto_opt=oneof=unions \
$GOPATH/src/github.com/heroiclabs/nakama-common/api/api.proto \
$GOPATH/src/github.com/heroiclabs/nakama-common/rtapi/realtime.proto

Release Process

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

yarn workspace <workspace> run build && yarn workspace <workspace> npm publish --access=public

License

This project is licensed under the Apache-2 License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].