Self-hosting your Moralis server

I clicked the link and this isnā€™t live yet? Iā€™ve applied for waitlist but at the moment what would be the way to get event syncs while self hosting own server? Would I have to write that logic myself?

Hi all. I am running into a speed bump where I cant seem to get live queries working. I have downloaded the demo files and my index.ts for the server looks like this:

import Moralis from 'moralis';
import express from 'express';
import cors from 'cors';
import config from './config';
import {
  // parseServer,
  parseTestServer,
  // parseIcoServer,
  // parseServeDemoServer,
} from './parseServer';
// import { parseDashboard } from './parseDashboard';
// @ts-ignore
import ParseServer from 'parse-server';
import http from 'http';

export const app = express();

Moralis.start({
  apiKey: config.MORALIS_API_KEY,
});

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(cors());

// app.use(`/server`, parseServer);
app.use(`/server`, parseTestServer);
// app.use(`/server`, parseIcoServer);
// app.use(`/server`, parseServeDemoServer);

// app.use(`/dashboard`, parseDashboard);

const httpServer = http.createServer(app);
httpServer.listen(config.PORT, () => {
  // eslint-disable-next-line no-console
  console.log(`Moralis Test Server is running on port ${config.PORT}.`);
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

and i am trying to subscribe in my react-native app using this code:

  const subscribeToMessages = async () => {
    let query = new Moralis.Query("Messages");
    query.equalTo("objectId", messagesPointer.id);
    // console.log("messagesPointer.id", messagesPointer.id)
    let subscription = await query.subscribe();
    subscription.on("create", notifyOnMessage);
    subscription.on("update", notifyOnMessage);
    // subscription.on('create', result => notifyOnMessage(result));
    return subscription;
  };

  const notifyOnMessage = (result) => {
    // console.log("notifyOnMessage", result);
    setUpdated(result);
    // notifyOnType(result);
  };

normal queries work, but live queries are not working. When i send a new chat message the new message isnt sent from the server to the app.

if i connect to the Moralis V1 server everything works fine. But if I connect to my self hosted server using the demo files, the live query doesnt seem to be working. Is anyone else having the same problem? if so how did you fix it? thanks in advance.
Iv managed to get most things working self hosted, but this is a major hurdle that i cant seem to figure out.

You should receive more info in the email on how to access streams api

forgot to add, this is the code for my parse server options:

// @ts-ignore
import { ParseServer } from 'parse-server';
const sendGridAdapter = require('parse-server-sendgrid-email-adapter');
import config from './config';
import MoralisEthAdapter from './auth/MoralisEthAdapter';

const objectIdSize = 24;
const sessionLength = 2 * 7 * 24 * 60 * 60; // 2 weeks

const defaultOptions = {
  masterKey: config.MASTER_KEY,
  javascriptKey: config.JS_KEY,
  restAPIKey: config.REST_KEY,
  cloud: config.CLOUD_PATH,
  auth: {
    moralisEth: {
      module: MoralisEthAdapter,
    },
  },
  emailAdapter: sendGridAdapter({
    apiKey: config.SG_KEY, // sendgrid api key
    from: config.SG_FROM_EMAIL, // from email address,
    passwordResetEmailTemplate: config.SG_PASSWORD_RESET_TEMPLATE, // sendGrid template ID
    verificationEmailTemplate: config.SG_VERIFY_EMAIL_TEMPLATE, // sendGrid template ID
  }),
  objectIdSize,
  sessionLength,
  // logLevel: 'warn',
  // silent: true,
  // verbose: 1,
};

export const parseTestServer = new ParseServer({
  databaseURI: config.TEST_DATABASE_URI,
  serverURL: config.TEST_SERVER_URL,
  appId: config.TEST_APPLICATION_ID,
  // port: config.PORT,
  ...defaultOptions,
});

could it be something to do with http vs https? just a wild guess

you donā€™t see any error in console/network tab? it just doesnā€™t work?

I have solved this. It seems you need to specify exactly which Classes you want to connect live query to. so once i added the following code to my parse server options it now works!

  liveQuery: {
    classNames: ['Messages', 'UserTyping'],
  },
2 Likes

Hi guys , this is my app url on Heroku https://morali-host.herokuapp.com/

but i dont know why i get Cannot GET /
and this is my log which looks fine
at=info method=GET path="/" host=morali-host.herokuapp.com request_id=b7b97ce1-b200-4864-84a3-3981935706f9 fwd="151.197.170.246" dyno=web.1 connect=0ms service=1ms status=404 bytes=415 protocol=https

Post your parse server properties and express code file.

thanks for the response,I have posted the parse server properties,and I use react app .I have another app running but I can not figure out whatā€™s wrong with it. I did not quite understand the express code file Thu

It seems to be responsive if you go to http://morali-host.herokuapp.com/server. Have you followed these instructions?

3 Likes

I have self-hosted Moralis server up on my local machine, How can I connect it to my local hardhat/ganache chain to receive event and talk to it ? The docs section ā€œsync on-chain databaseā€ is not documented yet. Iā€™m also on waiting list for ā€œstream APIā€. Iā€™m assuming the old ā€œfrpcā€ is no longer needed since both are on the same machine.

The ā€œsync on-chain databaseā€ section in dos is not documented yet.

That feature isnā€™t ready yet.

1 Like

Man I am super confused. I am trying to set up the self-hosted server Iā€™ve followed along the video tutorial but I am using my own NEXT JS app instead of the parse-server-migration-react-client repo.

I am also incorporating RainbowKit and followed the instructions on how to set that up. So far no issues there. But when it comes to the self-server I donā€™t get any logged in or authenticated user on my mongoDB whenever I connect my metamask.

Iā€™m looking at the authenticationModal.tsx file in the parse-server-migration-react-client repo that the video tutorial uses and in it they are importing Moralis from ā€˜moralis-v1ā€™ package. Which is confusing because I thought you only use moralis-v1 package if you want to use the cloud servers instead of the self-hosted ones?

This whole thing is super confusing.

if you use next js and you only want to authenticate users you donā€™t have to use this tutorial for self hosting, this tutorial for self hosting if if you only want to self host a version of the server similar to how moralis server is working now (with parse server). If you want to make your own backend with next js and use moralis v2 then you can do what without following this tutorial, there is a specific tutorial in documentation on how to use next js for authentication

1 Like

After some tinkering I did in fact set up RainbowKit Provider plus Authentication for my self-hosted Moralis Parse server! Lots of tinkering but if you combine this tutorial for self-hosting and this tutorial for rainbowKit: https://docs.moralis.io/docs/sign-in-with-rainbowkit, you can get a RainbowKit wallet authentication for your self-hosted Moralis parse server. Very, very pleased with the results. : )

1 Like

thanks , if i want to see the dapp from https://morali-host.herokuapp.com/

should i push the react files to the heroku?

For a React app, I would recommend Netlify or Vercel instead - theyā€™re more suitable for deploying frontend apps.

Also Heroku wonā€™t have free plans soon.

1 Like

in this tutorial video https://youtu.be/l2qTyc-V9cM Heroku is used to host the server online, but according to Heroku official website itā€™s free severs ends in November 28 2022. are there any replaceable service I can use for now, at least to a point where I have deposable income to pay for pay services?

thanks

you could also host it locally for tests

1 Like

You can try host in https://render.com which has freemium model

other than that, you can also host in cloud provider such as GCP, AWS, or Azure, which is more complicated to setup, but usually will cost very cheap or even give you free credits in the first 12 months

1 Like