I'm trying to use the code below but it doesn't work for me because the server doesn't connect and there's no error in the console.
var PORT=8080;
var http=require('http');
var http=require('swig');
var http=require('url');
exports.startServer=function (PORT){
var server=http.createServer(function(req,res){
var page=url.parse(req.url).pathname;
if(page=='/'){
res.writeHead(200,{'Content-Type':'text/html'});
res.write(swig.renderFile('templates/home.tpl',{
name :'user'
})
);
} else{
res.writeHead(404,{'Content-Type':'text/html'});
res.writeHead('<h1>Error 404 : page not found</h1>');
}
res.end();
});
server.listen(PORT);
console.log('Server running on'+ PORT);
}
The reason I think is that you have assigned the variable http for three times, which makes that only the last one takes effect.
Try to replace
var http=require('http');
var http=require('swig');
var http=require('url');
with
var http=require('http');
var swig=require('swig');
var url=require('url');
check any other server running on same port or not
if it is running, change port number
var port = process.env.PORT | 3000;
change this
var http=require('http');
var swig=require('swig');
var url=require('url');
Better to use express.
var express = require('express');
var app = express();
var port = process.env.PORT | 3000;
var server = app.listen(port,'0.0.0.0',function(){
var host = server.address().address;
var port = server.address().port;
console.log("Server listen to : "+host+":"+port);
});
Related
I'm trying to export socket.io server through multiple node script so i can emit notification on the same port.
Here is my main server.js file code:
var express = require('express'),
app = module.exports.app = express();
const options = {};
var server = http.createServer(app);
var io = require('socket.io').listen(server);
exports.io = io;
server.listen(3000, function() {
console.log('Node.js Global app is running...');
});
Below is other node script are runninig when i try to require server.js i get this error:
Error: listen EADDRINUSE 0.0.0.0:3000
server_tn.js
var express = require('express'),
app = module.exports.app = express();
var code_pays = path.basename(__dirname);
console.log('Node.js app is running...' + code_pays);
var main = require('./../main.js');
var importIo = require('./../server');
var io = importIo.io;
main.mainTraitement(code_pays);
You can't have more than one program listenning to a specific port.
Check if you have any program listening on port 3000, or if on your main.js you are also listenning on port 3000.
I'm running a server and all it needs to do is connect to a 3rd party public socket.io stream. I have it working in my react app but can't get it to work on my server. I'm using node and express.When I run I get no errors. Here is my code
var express = require("express");
var app = express();
var port = 3000;
var io = require('socket.io-client/dist/socket.io');
var socket = io.connect('http://socket.coincap.io', {transports: ['websocket']});
socket.on('trades', function(tradeMsg) {
console.log("worked");
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});
Just checked my code, I do something like the following which is a bit different than your code. Don't know if it's your solution but maybe that could helps you.
In a separate socket-io.js
module.exports = function (app, server) {
var socketIO = require('socket.io').listen(server, {'transports': ['websocket']});
global.socketIO = socketIO;
socketIO.set('origins', '*:*');
socketIO.sockets.on('trades', function (tradeMsg) {
console.log("worked");
});
};
Then in my www after var server = http.createServer(app); I've got:
require('socket-io')(app, server);
You can try installing socket.io-client using npm.
npm install socket.io-client --save
And try running the following code. It should work fine.
var express = require("express");
var app = express();
var port = 3000;
var socket = require('socket.io-client')('http://socket.coincap.io');
socket.on('trades', function(tradeMsg) {
console.log("worked");
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});
I have code snippet to explain what i am doing and what i want.
var express = require('express');
var http = require('http');
var app = express();
app.use('/', express.static(__dirname + '/static'));
var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app);
var binaryServer = new BinaryServer({server:server});
var ioServer = http.createServer(app);
var io = require('socket.io').listen(ioServer);
I can run node express and socket.io on same port.
ioServer.listen(8080, function(){
console.log('server running at localhost:8080');
});
Same can be done with node express and binaryServer.
server.listen(8080, function(){
console.log('server running at localhost:8080');
});
But i want to run node express, socket.io and binaryServer on same port express is running (8080 in this case).
Any suggestions ?
You would need to attach both the SocketIO and binaryServer to same http server instance then bring that single instance up.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var binaryServer = new BinaryServer({ server:server, path: '/binary'});
server.listen(8080, function(){
console.log('http/socket/binary server running at localhost:8080');
});
Set the path so binaryServer doesn't conflict with any of your apps. This path is required in the client connections too.
I'm hosting my application on openshift. I am using a custom domain. And socket.io wasn't able to download the client side script so I just used the cdn instead. But now it's not able to connect to a namespace. These are the errors it is giving me on the console log
This is my client side code on the .html page to download the client side script
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
and the .js index page to connect to the index namespace
var socket = io("http://www.loomius.com/index");
Here is my server side code
var express = require('express');
var app = express();
var http = require('http');
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var https = require('https');
// listening on the port
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
});
First instead of
var socket = io("http://www.loomius.com/index");
use this to solve issue with a need of cdn
var socket = io.connect("/");
Then on server use this instead
http = http.createServer( app ).listen( process.env.PORT, process.env.IP || "0.0.0.0", function() { // or define ip and port manually
var io = require( 'socket.io' )( http );
io.on('connection', function( socket ) {
// add event listeners here
}
});
In my application i need to connect two socket.io node applications.Using socket.io-client we can do like this.But i don't know how socket.io-client works and where to include that.
First Node Application
var express = require('express')
, http = require('http');
var app = express();
app.use(function (req, res) {
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
io.sockets.on('connection',function(socket){
socket.on('eventFiredInClient',function(data){
socket.emit('secondNodeAppln',data);// i need to get this event in my 2nd node application how can i do this by using socket.io-client
});
});
Second Node Application
var express=require('express');
var http=require('http');
var app=express();
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var serverAddress = '127.0.0.1';
var serverPort = 3000; //first node appln port
var clientio = require('socket.io-client');
var socket = clientio.connect(serverAddress , { port: serverPort });
socket.on('connect', function(){
console.log('connected');
});
socket.on('disconnect', function(){
console.log('disconnected');
});
var io = require('socket.io').listen(server);
server.listen(6509);
//here i need to get the 'secondNodeAppln' event raised in first node application.How can i do this.
You need to create a socket.io client in your first app:
var io = require('socket.io').listen(server); // this is the socket.io server
var clientio = require('socket.io-client'); // this is the socket.io client
var client = clientio.connect(...); // connect to second app
io.sockets.on('connection',function(socket) {
socket.on('eventFiredInClient',function(data) {
client.emit('secondNodeAppln', data); // send it to your second app
});
});
And in your second app, just listen for those events:
io.sockets.on('connection', function (socket) {
socket.on('secondNodeAppln', function(data) {
...
});
});
There's a bit of a race condition because the code above doesn't wait for a connect event on the client socket before passing events to it.
EDIT see this gist for a standalone demo. Save the three files to a directory, start the servers:
node serverserver &
node clientserver
And open http://localhost:3012 in your browser.