簡體   English   中英

如何運行快速服務器監聽MongoDB並在Heroku中偵聽套接字io消息?

[英]How do I run an express server listening for MongoDB and listening for socket io messages in Heroku?

我正在為Heroku設置一個服務器后端,它正在監聽google Oauthentication,監聽要保存到Mongo DB的消息,以及使用socket io監聽要發送給所有連接用戶的聊天消息。

只是我的應用程序是使用React構建的。

我在本地主機上運行時能夠按預期運行這些功能,但我必須設置socket io使用的http服務器實例和兩個不同端口上所有其他功能使用的快速服務器。

但是Heroku只允許我訪問一個端口。

我一直在網上尋找不同的解決方案。

我最好的選擇似乎是試圖讓套接字io通過同一個端口發送消息,但是當我這樣做時,我收到一個錯誤,指出我不能讓兩個不同的服務器訪問同一個端口。

因此,即使我使用相同的express實例來為socket io創建我的http服務器實例,它們也被認為是單獨的實例。

我的主要問題是,socket io需要一個http服務器實例,而其他所有功能都可以使用快速服務器運行。

我想知道是否有人在嘗試部署到Heroku時遇到過這樣的問題? 我一直在尋找有類似問題但沒有運氣的人。 我真的可以在這個問題上使用一些指針。

我在下面發布我的服務器代碼。

編輯5/21/2019:我決定減少相關代碼,因為要解析的內容與問題無關。

require(`dotenv`).config();
require("./config/connection");

const express = require('express');
const app = express();

// Routes to Mongo DB
const chatRouter = require('./routes/chat');

// Passport
const passport = require("passport");
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('./config/keys');

// socket io
const server = require('http').createServer(app);
const io = require('socket.io')(server);

// google
const routes = require("./routes");
const cookieSession = require('cookie-session');
const path = require('path');

const db = require('./model');

const PORT = process.env.PORT || 3001;

// Tells Express to Read the data sent in JSON format
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

// Tells Express to allows data to be sent across different origins
// required by CORS policy
app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

// Implementation of other features such as saving chat messages to database
// and implementing passport
...

// Chat implementation using Socket IO

io.on('connection', client => {

  // implementing chat feature
  ...

});

server.listen('8000', () => {
  console.log('listening on port 8000');
});

// alternatively we can attempt to listen on the port specified by Heroku
// server.listen(PORT, () => {
//   console.log(`listening on port ${PORT}`);
// });

// implementing more features, such as Google Oauth
// and other api routes
...

// Express application instance listening on port specified by Heroku
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

我感謝您對此問題的任何幫助。

謝謝!

所以看起來我的問題已經被問到並回答了。

你可以在以下鏈接找到答案, Socket.io + NodeJS不適用於Heroku

我將在這里給出一個簡短的總結。

Heroku要求我們的應用程序在從前端到后端進行通信時在同一端口上運行API的所有功能,反之亦然。 所以我第一次嘗試指定兩個不同的端口在本地工作,但在部署到Heroku時失敗了。

然后我嘗試為兩個服務器實例使用相同的端口。


// implementing express
const express = require('express');
const app = express();

// uses port provided by Heroku OR port 3000
const PORT = process.env.PORT || 3000;

...

// implementing Socket IO
const server = require('http').createServer(app); // creates an instance of an http server
const io = require('socket.io')(server);

...

io.on('connection', client => {

    // server side chat features implemented here

}

server.listen(PORT); 

...

app.listen(PORT); // Express is returning a different instance of the http server

解決方案比我想象的簡單。

由於app.listen(PORT)返回http服務器的實例,我們可以將它用於Socket IO。 在點擊之前我必須首先查看一些示例代碼。 Full Stack Web Development Bootcamp中的一位老師將我鏈接到這個git hub存儲庫,演示了在將應用程序部署到Heroku時如何使用套接字IO。

請參閱以下鏈接, https://github.com/heroku-examples/node-socket.io/blob/master/server.js

上面示例中的實現看起來有點不同,但實際上正在執行與我將在下面的示例中演示的相同的操作,


// get Express library and create Express application
const express = require('express');
const app = express();

// get socket io library
const SocketIO = require('socket.io');

// uses port provided by Heroku OR port 3000
const PORT = process.env.PORT || 3000;

...

// This time we store the HTTP Server instance in a variable
// when we tell the Express application to listen on the specified port
const server = app.listen(PORT); 

// and use that instance to implement our socket io connection
const io = SocketIO.listen(server);

io.on('connection', client => {

    // server side chat features implemented here

}

希望這個解釋有助於其他人理解並避免我面臨的問題。

並且一定要查看發布的鏈接,因為我發現它們有助於理解我面臨的問題以及如何解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM