2

I have a platform running with Apache + PHP which I distribute to several people through subdomains, Ex: platform.subdomain1.com, platform.subdomain2.com,etc.. . And I would like one of the features of this platform to be Video streaming and i chose to do this with Node.js + socket.io. I don't have much Node experience but I managed to make streaming itself work. I basically have a directory called stream with app.js, index.html and two html files: one to stream the video and one to view.

My problem: I would like to merge the two so that I can link to these streaming and viewing pages so that each user with their subdomain has their own streaming. I wonder if there is any way to do it and what it would be.

I could create a directory with the all node streaming files inside each subdomain and create a new instance for each one, like this:

var app = new express();
const http = require("http").Server(app)
http.listen('platform.subdomain1.com',3000);

So that I could link my platform to the address: platform.subdomain1.com/stream:3000

but I'm not sure if it is right to do this or if there is another way to do it. If anyone can help me thank you very much!

My App.js

var express = require("express");
var app = new express();
const http = require("http").Server(app)
var io = require("socket.io")(http);
app.use(express.static(__dirname + "/public"));
app.get('/', function(req,res){
    res.redirect('index.html');
});

io.on('connection', function(socket){
    socket.on('stream', function(image){
        socket.broadcast.emit('stream', image);
    });
});

http.listen(3000); 
3
  • I can't really tell what you're asking. You can freely use socket.io along with Express just fine. No need for a separate server for socket.io. Commented Sep 5, 2019 at 2:35
  • 1
    Take a look at PM2 and muti-app start scripts. It might be what you're after, but as others have said, it's not really clear from your question. Commented Sep 5, 2019 at 15:18
  • Thank u @Richard Dunn! I think that's exactly what I need, sorry for the confusing question. I'm new to Node so some concepts are still a little confusing for me Commented Sep 5, 2019 at 16:37

1 Answer 1

2

yes, this is the right way to work with socket.io and express together

  • create express server
  • bind express server to socket.io
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(80);
// WARNING: app.listen(80) will NOT work here!

app.get('/ping', function (req, res) {
  res.send("pong")
});
app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Advantage with express:

You will have support for ping/pong or health check in case of AWS load balancer health check or any platform that route request base on target health as socket.io does not support health check in AWS ALB.

you check official documentation suggested by socket.io.

socket.io-Using-with-Express

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

Comments

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.