Reputation: 1056
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);
Upvotes: 2
Views: 93
Reputation: 60124
yes, this is the right way to work with socket.io
and express together
express
serversocket.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.
Upvotes: 2