I use NodeJS both as the server and the client. (No web browsers)
Server seems to be working, but client does not connect. I tried to set the port in the options but it did not work. I try to connect to the port 3000 over telnet and it connects to something, so the server is listening.
What am I missing here?
Server:
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var io = require('socket.io')();
var port = 3000;
server.listen(port,"127.0.0.1", () => {
console.log('Server listening at port %d', port);
});
Client:
const io = require('socket.io-client');
const socket = io("http://127.0.0.1:3000",{reconnect:false});
socket.on('connect', function () {
console.log('connected to the server');
});
As mentioned on the offical socket-io github page, you need to pass the http-server instance to the socket-io server:
...
var server = require('http').createServer(app);
var io = require('socket.io')(server);
...
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
}
});
Can we start the TCP server using the port of express.js server, i know socket.io can do that using the following code
var express = require('express');
var app = express();
var server = app.listen(3000, function() {
console.log("server started on port 3000");
});
var io = require('socket.io').listen(server);