Node.js multiple apps - javascript

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);

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

Related

How to redirect app's data stream to browser in real time?

My node app can read stream from kafka producer and console.log it to terminal in real time. But I would like to update it in my web app the same way (real time). How can I implement it?
I start my app with 'node index.js' command (or npm start).
index.js:
'use strict';
var express = require('express'),
app = express(),
server,
data = [];
...
consumer.on('message', function (message) {
console.log(message);
data.push(message);
//global.location.reload();
});
...
app.get('/', function(req, res){
res.send(data);
});
server = app.listen(3002, function(){
console.log('Listening on port 3002');
});
I think, that I need modify res.send(data) or add some code to on('message') event.
You should keep connection between client and server.
Try this socket.io package to update message in realtime.

Using Socket.io With Express 4 on Different Pages and Understand Socket.io example code

First of all I know a similar question has been asked here, however, I did not understand the answer and my question is worded slightly differently so I am wondering if I can get a more clear answer.
The Express Generator starts an app like so:
app.js
var express = require('express');
var routes = require('./routes/index');
var users = require('./routes/users');
var io = require('socket.io');
(etc.)
I want to use Socket.io so I also install it and require it and following the Socket Docs do this
var server = express.createServer();
var io = require('socket.io')(server);
However, all of my routes, where I really want to handle socket.io events, take place on my routes/index.js page. How do I use socket.io with my router, which is on a different page? How do I pass it in when my server instance was started on app.js (a different page)?
Furthermore, the socket.io docs list two ways to get started with express. The first example shows:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
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);
});
});
And the second one shows:
var app = require('express').createServer();
var io = require('socket.io')(app);
app.listen(80);
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);
});
});
Can anyone explain the difference between using the "Express Framework" and "With Express 3 /4" as the documentation differentiates. I would also love to know why the examples use express.createServer() (when a generated express app does not) and why the other example uses both the express and http node modules. Thank you
A working piece of code would be as given below. The important thing here is do all the work with io initialization and io handling once you start listenting on express server. Hope this helps. :-)
var app = require('express')();
var socket_io = require('socket.io');
app.io = socket_io();
// setup app routes and express settings starts ...
// ....
// ... setup app routes and express settings ends
var server = app.listen(app.get('port') || 3000);
app.io.attach(server);

Express and socket.io chat?

I can set up the chat no problem, but my question is how do I get the chat to work on the default port:80 where my main site is?
First thing that comes to mind is iframing it?
Here is my server.js code, one thing to note I don't really like jade so I am going to convert that into plain HTML. Also for the chat to work it has to be on any port other than :80 so I am not quite sure how to get it to work on that main port, other than iframing?
So my question is obviously what are my options in getting the express server to work on the main port?
// Start server
var express = require("express");
var app = express();
var port = 3700;
// Directory
app.use(express.static(__dirname + '/chat'));
// Socket.io integration
var io = require('socket.io').listen(app.listen(port));
// Render content
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.get("/", function(req, res){
res.render("page");
});
// Recieve msg and send
io.sockets.on('connection', function (socket) {
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
You dont realy need node-expres-jade on the port 80 for these aproach.
Node can just run the socket server (alone). Forget express/jade here.
And apache or nginx can handle all de client-side files (html, js , css ...).
add the var socket = io.connect('httt://yoururl:port'); on any html an it should work.

Configuring 'simplest' node.js + socket.IO + Express server

Realized after setting up a simple node.js socket.IO server that it isn't sufficient to handle even the simplest webpages containing script tags.
So I investigating express which is a simple web framework for node.js.
After looking thru the express documentation http://expressjs.com/guide.html
I was still confused as to how I simply combine express with socket.IO on a node.js server.
Couple hours of googling later I came across this tutorial
https://www.digitalocean.com/community/articles/how-to-install-express-a-node-js-framework-and-set-up-socket-io-on-a-vps
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server); // this tells socket.io to use our express server
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
console.log("Express server listening on port 3000");
io.sockets.on('connection', function (socket) {
console.log('A new user connected!');
socket.emit('info', { msg: 'The world is round, there is no up or down.' });
});
My question is, would anyone reading this configure their server differently?
I don't need anything special, no session handling etc, just the ability to serve html pages containing links to external CSS and javascript files.
Remove the first app.configure wrapper but leave it's contents. It is useless in general, but especially if you don't pass an argument to it.
Remove methodOverride and bodyParser as you aren't using them
Thanks for all the replies. Finally have something that works and am posting so someone else may benefit. My first attempt(above) was obviously NOT the simplest solution:)
//npm install express
//npm install socket.io
var express = require('express');
var server = express.createServer();
server
.use( server.router )
.use( express.static(__dirname+'/public') )
.get('/api', function(req, res) {
res.write('API');
});
server=server.listen(3000);
var io = require('socket.io');
var socket = io.listen(server);
socket.on('connection', function (client){
// new client is here!
});

Where can I find a nodejs/javascript client combination example, showing data being loaded in the client from the server, modified, and sent back?

Any working example, using the latest version of nodejs, will work; ideally, it's as simple as possible.
to use create a folder, npm install express socket.io then place in the three files, and 'node app.js'.
layout.jade
!!! 5
title=title
body!=body
index.jade
script(src='http://cdn.socket.io/stable/socket.io.js')
script
//create socket
var socket = new io.Socket();
//connect socket
socket.connect();
//on data recieved
socket.on('message', function(data){
//log data
console.log( data );
//modify data
data.modified = true;
//return data
socket.send(data);
});
app.js
// expressjs.com, a web framework
var express = require('express');
// socket.io, real time communications
var io = require('socket.io');
//create web server
var app = module.exports = express.createServer();
//configure web server
app.configure( function () {
app.set('views', __dirname);
app.set('view engine', 'jade');
app.use(app.router);
});
//handle requests for /
app.get('/', function (req, res, next) {
res.render('index', {
title: 'socket.io test'
});
});
// listen on port 8080
app.listen( 8080 );
console.log("Express server listening on port %d", app.address().port);
// attach socket.io to the web server
var socket = io.listen( app );
// when a client connects
socket.on('connection', function(client){
//send the client some data
client.send({ data: [1,2,3] });
// when the client sends back data
client.on('message', function (msg){
// log the data
console.log( msg );
});
});
Maybe, nowjs is that you want. It's provide simple API to call server functions from client.

Categories