简体   繁体   中英

Deploy an application that uses a node.js server on Firebase

I'm implementing a web app using Vue. In the application, I use "pusher" to create a real-time multi-user communication. In particular I set up a node.js server listening on the port 5000 of a specific device in my local network. The application works in my home network (I'm able to interact with different devices connected). Now I want to deploy the application on Firebase.

Below there is the code related the definition of the pusher instance, with the "authEndpoint" property that specifies the path to which a user has to send an http post request in order to subscribe itself to the shared channel. When I deploy the application on firebase , how should I manage the authEndpoint property and the position of the node.js server in order to make the application working on the internet?

 import Pusher from 'pusher-js'
  const pusher = new Pusher('*************', {
    cluster: 'eu',
    encrypted: true,
    authEndpoint: '192.168.1.237:5000/pusher/auth'
  })

This is the code related to the server:

const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const app = express()
const Pusher = require('pusher')
const crypto = require('crypto')
const cors = require('cors')

//initialize Pusher with your appId, key and secret
const pusher = new Pusher({
   appId: '****',
key: '**************',
secret: '************',
cluster: 'eu',
encrypted: true
})

// Body parser middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cors())
// The code below helps to fix any potential CORS issue.
app.use((req, res, next) => {
  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', '*')
  // Request methods you wish to allow
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true)
  // Pass to next layer of middleware
  next()
})

// Index API route for the Express app
app.get('/', (req, res) => {
  res.send('Welcome')
})

// API route used by Pusher as a way of authenticating users
app.post('/pusher/auth', (req, res) => {

  let socketId = req.body.socket_id
  let channel = req.body.channel_name
  // Generate a random string and use as presence channel user_id
  let presenceData = {
    user_id: crypto.randomBytes(16).toString('hex')
  }
  let auth = pusher.authenticate(socketId, channel, presenceData)
  res.send(auth)
})

// Set port to be used by Node.js

app.set('port', (5000))

app.listen(app.get('port'), () => {
  console.log('Node app is running on port', app.get('port'))
})

You won't be able to use the Pusher library the way you want when deployed to Firebase as a Cloud Function. Cloud Functions are short-lived stateless containers that are spun up on demand in response to events and user requests. The Pusher library you're using establishes a long-lived connection and listens, something that can't done since a Cloud Function is torn down immediately after it completes.

You might instead want to look at Pusher webhooks which could be configured to point to a Cloud Function's HTTP endpoint and may give you some of what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM