Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach socket.io to google firebase app functions?

I have the following code in index.js file located into functions folder in my google firebase proyect:

net=require('express')()
net.get('/',function(req,res){res.sendFile(__dirname+'/slave.htm')})
exports.run=require('firebase-functions').https.onRequest(net)
require('socket.io').listen(net).on("connection",function(socket){})

But when I execute \gfp1>firebase deploy in command prompt, this give me that errors:

You are trying to attach socket.io to an express request handler function. Please, pass a http.Server instance.

Yes, and I pass and http server instance in the following code:

net=require('firebase-functions').https.onRequest((req,res)=>{res.send("socket.io working!")})
exports.run=net;require('socket.io').listen(net).on("connection",function(socket){})

It gives me again the following error:

You are trying to attach socket.io to an express request handler function. Please, pass a http.Server instance.

And I try attaching socket.io to firebase functions with that code:

net=https.onRequest((req,res)=>{res.send("socket.io working!")})
exports.run=require('firebase-functions').net
require('socket.io').listen(require('firebase-functions').net).on("connection",function(socket){})

And that gives this error:

https is not defined

When I run this code in localhost:

app=require('express')()
app.get('/',function(req,res){res.sendFile(__dirname+'/slave.htm')})
net=require('http').createServer(app);net.listen(8888,function(){console.log("Server listening.")})
require('socket.io').listen(net).on("connection",function(socket){})

The console emmit \gfp>Server listening., and when I go to url http://127.0.0.1:8888, it works sending an html file to navigator, as I expected:

<script>
document.write("File system working!")
document.body.style.backgroundColor="black"
document.body.style.color="white"
</script>

But the problem happens when I try to convert net=require('http').createServer(app);net.listen(8888,function(){console.log("Server listening.")}) to net=exports.run=require('firebase-functions').https.onRequest((req,res)=>{res.send("Firebase working!")}), it seems to be impossible.

like image 919
Cronex Avatar asked Dec 18 '22 08:12

Cronex


1 Answers

You can't run code to listen on some port with Cloud Functions. This is because you aren't guaranteed to have a single machine or instance running your code. It could be distributed among many instances all running concurrently. You shouldn't know or care if that happens - Cloud Functions will just scale to meet the needs placed on your functions.

If you deploy an HTTP type function, it will automatically listen on the https port for the dedicated host for your project, and you can send web requests to that.

If you want to perform transactions over a persistently held socket, use the realtime database client write values in the database, then respond to those writes with a database trigger function that you write. That function can send data back to the client by writing something back to the database in a location that's being listened to by the client.

like image 98
Doug Stevenson Avatar answered Jan 05 '23 17:01

Doug Stevenson