colyseus / colyseus-social

Authentication and Social features
https://docs.colyseus.io/tools/colyseus-social/
MIT License
21 stars 17 forks source link

Unable to connect to mLab MongoDB addon from Heroku #18

Closed dclipca closed 4 years ago

dclipca commented 4 years ago

As the title says, I am unable to connect to the mLab MongoDB addon from Heroku. Would be glad to receive any suggestions. My environment variables are as follows:

JWT_SECRET ZSy5CfFW1GiM8xsFtSHA MONGODB_URI mongodb://heroku_0t3t4std:2ffktbkt17r7mc9m4lce1b3prm@ds149433.mlab.com:49433/heroku_0t3t4std MONGO_URI mongodb://heroku_0t3t4std:2ffktbkt17r7mc9m4lce1b3prm@ds149433.mlab.com:49433/heroku_0t3t4std NODE_OPTIONS --max_old_space_size=20480 NPM_CONFIG_PRODUCTION false

The index.ts:

import http from "http";
import express from "express";
import socialRoutes from "@colyseus/social/express"
import cors from "cors";
import { Server } from "colyseus";
import { monitor } from "@colyseus/monitor";
import GlobalRoom from "./src/room";

const app = express()
app.use(cors());
app.use(express.json())
const server = http.createServer(app);
const gameServer = new Server({
  server,
});
gameServer.define('room', GlobalRoom);

app.use("/", socialRoutes);
app.listen(8080);

// register colyseus monitor AFTER registering your room handlers
/* app.use("/colyseus", monitor()); */

/* const port = Number(process.env.PORT || 2567); */
const port = 7070;
gameServer.listen(port);
console.log(`Listening on ws://localhost:${ port }`)

Logs:


Listening on ws://localhost:7070
2020-04-27T03:15:57.066759+00:00 app[web.1]: (node:23) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2020-04-27T03:15:57.152939+00:00 app[web.1]: Error connecting to database:  MongoNetworkError: failed to connect to server [ds149433.mlab.com:49433] on first connect [MongoNetworkError: connection timed out
2020-04-27T03:15:57.152941+00:00 app[web.1]: at connectionFailureError (/app/node_modules/mongodb/lib/core/connection/connect.js:406:14)
2020-04-27T03:15:57.152942+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connect.js:294:16)
2020-04-27T03:15:57.152942+00:00 app[web.1]: at Object.onceWrapper (events.js:416:28)
2020-04-27T03:15:57.152943+00:00 app[web.1]: at Socket.emit (events.js:310:20)
2020-04-27T03:15:57.152943+00:00 app[web.1]: at Socket.EventEmitter.emit (domain.js:482:12)
2020-04-27T03:15:57.152944+00:00 app[web.1]: at Socket._onTimeout (net.js:479:8)
2020-04-27T03:15:57.152944+00:00 app[web.1]: at listOnTimeout (internal/timers.js:549:17)
2020-04-27T03:15:57.152945+00:00 app[web.1]: at processTimers (internal/timers.js:492:7) {
2020-04-27T03:15:57.152945+00:00 app[web.1]: name: 'MongoNetworkError',
2020-04-27T03:15:57.152945+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {}
2020-04-27T03:15:57.152946+00:00 app[web.1]: }]
2020-04-27T03:15:57.152946+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
2020-04-27T03:15:57.152947+00:00 app[web.1]: at Pool.emit (events.js:310:20)
2020-04-27T03:15:57.152947+00:00 app[web.1]: at Pool.EventEmitter.emit (domain.js:482:12)
2020-04-27T03:15:57.152948+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:561:14
2020-04-27T03:15:57.152949+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:994:11
2020-04-27T03:15:57.152949+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/connect.js:31:7
2020-04-27T03:15:57.152950+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:264:5)
2020-04-27T03:15:57.152950+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connect.js:294:7)
2020-04-27T03:15:57.152951+00:00 app[web.1]: at Object.onceWrapper (events.js:416:28)
2020-04-27T03:15:57.152951+00:00 app[web.1]: at Socket.emit (events.js:310:20)
2020-04-27T03:15:57.152951+00:00 app[web.1]: at Socket.EventEmitter.emit (domain.js:482:12)
2020-04-27T03:15:57.152952+00:00 app[web.1]: at Socket._onTimeout (net.js:479:8)
2020-04-27T03:15:57.152952+00:00 app[web.1]: at listOnTimeout (internal/timers.js:549:17)
2020-04-27T03:15:57.152953+00:00 app[web.1]: at processTimers (internal/timers.js:492:7) {
2020-04-27T03:15:57.152953+00:00 app[web.1]: name: 'MongoNetworkError',
2020-04-27T03:15:57.152954+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {}
2020-04-27T03:15:57.152954+00:00 app[web.1]: }
2020-04-27T03:16:20.341986+00:00 heroku[web.1]: State changed from crashed to starting
2020-04-27T03:16:20.337781+00:00 heroku[web.1]: State changed from starting to crashed
seiyria commented 4 years ago

You should edit out your username and password from those urls.

dclipca commented 4 years ago

You should edit out your username and password from those urls.

It's ok, the database is completely blank and I am using the free version of the addon for testing.

dclipca commented 4 years ago

@seiyria, what do you thin might be the issue?

dclipca commented 4 years ago

So the fix was a combination of me: 1) Removing a proxy the proxy I was using 2) Using

const port = Number(process.env.PORT || 2567);

instead of

const port = 2567;

The second I think, was the fix.

On the client-side I am now connecting using:

ws://overworld-io-server.herokuapp.com

instead of


ws://overworld-io-server.herokuapp.com:{the_specified_port}