1

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'))
})
2
  • I'm not sure I understand the question. stackoverflow.com/help/how-to-ask Commented Mar 30, 2020 at 14:22
  • I have problems to make the application working on the internet. In particular I don't know where to place the node.js server, that manages the subscription to the shared pusher channel (used to share informations between users ), when i deploy the application on Firebase. In my home network the node.js server is listening on 192.168.1.237:5000. @AlexMA Commented Mar 30, 2020 at 14:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If I use a different hosting server such as cloudno.de or nodejitsu , may I resolve the problem without using Pusher webhooks? @Michael Bleigh
If you find a hosting service that keeps a persistent process always running, yes you can likely resolve this without using webhooks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.