I have this socket.io:
var express = require('express');
var app = express();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let http2 = require('http').Server(app);
let io2 = require('socket.io')(http2);
io.on('connect', function(socket) {
app.use(express.static(__dirname + '/one_dir'));
});
io2.on('connect', function(socket) {
app.use(express.static(__dirname + '/another_dir'));
});
http.listen(4000, function(){
console.log('check *:4000');
});
http2.listen(5000, function () {
console.log('& check *:5000');
});
I am new in node.js(and in socket too), so I need that on 127.0.0.1:4000 was index.html from /one_dir, and on 127.0.0.1:5000 was index.html from /another_dir,
Can you help me to solve this problem?
Related
I would like to know if it's possible to create multiple socket servers on differents url routes on only one nodejs server.
I'm trying to have two differents server instance running on differents url
like mysite/instanceA and mysite/instanceB
When i tried to do that with port and not url. The application start well and on the port 3000 and 3001, i got two distincts application. It's great.
let express = require('express');
let http = require('http');
let socket = require('socket.io');
let workspace = require('./src/workspace');
class Server {
constructor(port){
this.httpServer = http.Server(app);
this.io = socket(this.httpServer);
workspace.newWorkspace(this.io , app);
this.httpServer.listen(port, function(){
console.log('App started on : ' + port);
});
}
}
let app = express();
app.use(express.static("public"));
new Server(3000);
new Server(3001);
But, i want do that with url and not port, so i tried something like that. I know why is not worked but i didn't know how to realize my aim.
Example:
let express = require('express');
let http = require('http');
let socket = require('socket.io');
let workspace = require('./src/workspace');
const port = process.env.PORT || 3000 ;
class Server {
constructor(){
this.httpServer = http.Server(app);
this.io = socket(this.httpServer);
workspace.newWorkspace(this.io , app);
this.httpServer.listen(port, function(){
console.log('App started on : ' + port);
});
}
}
let app = express();
app.use(express.static("public"));
app.get("/", function(req, res){
new Server();
});
app.get("/a", function(req, res){
new Server();
});
I have two EC2 instances, and I am using socket.io to establish communication between them.
In my First Server I have the following files
server1.js
........
........
var loadData = require("./client/getData");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', loadData);
var port = Number(process.env.PORT || 5000);
app.listen(port);
var server = require("https").Server(app);
var io = require("socket.io-client");
app.set('socketio', io);
..........
getData.js
......
router.get('/getMetaData', function(req, res){
var io = req.app.get('socketio');
var socket = io.connect('https://172.43.3.2:8080');
socket.on('metaData', function(data){
console.log("MetaData:", data);
res.json(data);
});
});
module.exports = router;
In my Second Server i have the following
server2.js
var express = require('express');
var app = express();
var server = require("https").Server(app);
var io = require("socket.io")(server);
app.get('/', function (req, res) {
res.send('Hello World');
});
var port = Number(process.env.PORT || 5000);
app.listen(port);
var metaData = {
"block1":["id1","id2"]
}
io.on('connection', function(socket){
console.log("Received Connection");
socket.emit('metaData', metaData);
});
console.log("Server Running on:", process.env.PORT);
My request to the route /getMetaData is resulting in "504 Gateway Time-out", I am not sure what I am doing wrong or missing out on
I am trying to set-up an express server with socket.io that will allow node.js clients and browser clients to connect. The browser connects with no problem. The node.js client using socket.io-client give an error:
unhandled socket.io url
Server:
var express = require('express'),
io = require('socket.io');
var app = express();
var host = 'localhost';
var port = process.env.PORT || 8080;
var server = app.listen(port, function() {
console.log('Gulp is starting my app on PORT: ' + port)
});
io = io.listen(server);
app.use('/', express.static(__dirname + '/'));
io.on("connection", function(socket) {
socket.on('clientMessage', function(jsonData, from) {
socket.emit('serverMessage', 'Got a message!');
console.log('Data being sent from', from, 'is:\n' + jsonData);
});
});
Client:
var io = require('socket.io-client')
var socket = io.connect('http://192.168.1.222:8080', {reconnect: true});
socket.emit('clientMessage', 'Hello', 'Pi-Voyager');
The issue was with the way I was requiring my dependencies. It worked on other versions but on my current version it did not.
node -v
v0.12.4
"express": "^4.13.1",
"socket.io": "^1.3.6",
"socket.io-client": "^1.3.6"
With these versions, the following code works:
Server
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 8080;
server.listen(port, function() {
console.log('Gulp is starting my app on PORT: ' + port)
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on("connection", function(socket) {
socket.on('clientMessage', function(jsonData, from) {
socket.emit('serverMessage', 'Got a message!');
console.log('Data being sent from', from, 'is:\n' + jsonData);
});
});
Client
var io = require('socket.io-client')
var socket = io.connect('http://10.1.0.47:8080', {reconnect: true});
socket.emit('clientMessage', 'Hello', 'Pi-Voyager');
I am trying to sent a basic message when users connect and disconnect. Later i want to sent parameters back and forth. But with the following code i do not get the messages print out on the server when they connect/disconnect.
When i replace io.on with server.on i can get the message 'User connected' but not user disconnected when i refresh/close the page. This however is buggy, since not everytime i refresh it logs the message and on first time it sometimes log the message multiple times.
server.js
var //io = require('socket.io'),
http = require('http'),
express = require('express');
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
var app = express();
app.use(express.static(__dirname + '/views'));
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
});
server = http.Server(app);
//io = io.listen(server);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('User connected');
io.on('disconnect', function(socket) {
console.log('User Disconnected');
});
});
server.listen(PORT, HOST, null, function() {
console.log('Server listening on port %d in %s mode', this.address().port, app.settings.env);
});
New code:
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
server.listen(PORT);
app.use(express.static(__dirname + '/views')); //need this for my directory structure.
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
}); //Need this to call my page.
io.on('connection', function( client ){
console.log('Connected');//this does not work.
io.on('disconnect', function( client) {
console.log(client);
});
});
Try reconfiguring your socket, and server instantiation:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(process.env.PORT || 3000);
io.on('connection', function( client ) {
console.log(client) //this will now work
client.on('disconnect', function( id ) {
console.log('user with ID ' + id + ' has disconnected');
});
});
First of all you need to be sure if you have included the client side socket.io files.
<script src="/socket.io/socket.io.js"></script>
Then you need to initialize the socket connection.
<script>
var socket = io();
</script>
The above changes will be in your index.html file.
Now in server.js file,
io.sockets.on('connection', function(socket) {
console.log("User connected");
socket.on('disconnect', function() {
console.log('Got disconnect!');
});
});
I have an express node app, and I'm trying to keep my code neat by not having all the socket.io stuff in app.js
I don't know the best way to go about this. Here is my initial thought which doesn't feel like the cleanest one
// app.js
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, url = require('url')
, somePage = require('./routes/somePage.js')
, path = require('path');
app.configure(function(){...});
app.get('/', somePage.index);
and the route
// somePage.js
exports.index = function (req, res, server) {
io = require('socket.io').listern(server)
res.render('index',{title: 'Chat Room'})
io.sockets.on('connection', function(socket) {
...code...
}
}
I feel like I'm close but not quite there
I don't know if I'm reading that right but it looks like you are starting a socket server on every request for /, which I'm frankly a little surprised works at all.
This is how I'm separating out the socket.io code from app.js (using express 3.x which is a bit different than 2.x):
// app.js
var express = require('express');
var app = express();
var server_port = config.get('SERVER_PORT');
server = http.createServer(app).listen(server_port, function () {
var addr = server.address();
console.log('Express server listening on http://' + addr.address + ':' + addr.port);
});
var sockets = require('./sockets');
sockets.socketServer(app, server);
// sockets.js
var socketio = require('socket.io');
exports.socketServer = function (app, server) {
var io = socketio.listen(server);
io.sockets.on('connection', function (socket) {
...
});
};
Hope that helps!
a similar approach is to pass app into index.js file and initiate http and socketio server there.
//app.js
//regular expressjs configuration stuff
require('./routes/index')(app); //all the app.get should go into index.js
Since app is passed into index.js file, we can do the app.get() routing stuff inside index.js, as well as connecting socketio
//index.js
module.exports = function(app){
var server = require('http').createServer(app)
,io = require('socket.io').listen(server);
app.get('/', function(req, res){
});
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
io.sockets.on('connection', function(socket){
socket.on('my event', function(data){
console.log(data);
});
});
io.set('log level',1);
//io.sockets.emit(...)