---
description: This is a sample of how you can get this setup on nodejs.
---

# NODEJS BASE SETUP

```javascript
global.navigator = { appName: "nodejs" };
global.window = {};
const jsencrypt = require("jsencrypt");
const WebSocket = require("ws");

const loginUserId = "SOME USERID";
const loginUserPass = "SOME PASSWORD";

const websocketURI = "wss://fitest.fundamentalinteractions.com:8088/";

const login = (email, password) => {
  let encryptedPassword = ws.encrypt.encrypt(password);
  ws.send(
    JSON.stringify({
      type: "login",
      userid: email,
      pass: encryptedPassword,
    })
  );
};

const getUsers = (userid) => {
  ws.send(
    JSON.stringify({
      type: "queryexistingusers",
      userid: userid ? userid : undefined, 
      // if userid is not set you get all user
    })
  );
};

let wrapper = {
  connect: () => {
    wrapper.connection = new WebSocket(websocketURI, {
      perMessageDeflate: false,
      rejectUnauthorized: false,
      handshakeTimeout: 30000,
    });

    wrapper.connection.on("open", () => {
      console.log("connection opened");
      ws.send(JSON.stringify({ type: "challenge" }));
    });

    wrapper.connection.on("message", (data) => {
      data = JSON.parse(data);
      if (data.type == "challenge") {
        let user = loginUserId;
        let pass = loginUserPass;
        let encrypt = new jsencrypt();
        ws.encrypt = encrypt;
        encrypt.setPublicKey(data.key);
        ws.encrypt = encrypt;
        login(user, pass);
      } else if (data.type == "heartbeat") {
        ws.send(JSON.stringify({ type: "heartbeat" }));
      } else if (data.type == "login") {
        console.log(data);
        getUsers(loginUserId);
      } else if (data.type == "queryexistingusers") {
        console.log(data);
      }
    });

    wrapper.connection.on("close", () => {
      console.log("CLOSE AND RECONNECT");
      delete wrapper.connection;
      wrapper.connect();
    });

    wrapper.connection.on("error", (e) => {
      console.log(e);
    });

    global.ws = wrapper.connection;
  },
};

wrapper.connect();

```
