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);
});
Related
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);
...
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 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 am not able to run socket.io code in node.js, console.log() is also not displaying when running the code. Below is the code.
app.js
var express = require('express');
var http = require('http');
var app = express();
app.set('port', process.env.PORT || 3000);
app.post('/testStream',test.testStream);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
module.exports.appServer = server;
and I have created a test.js file where I am accessing this exported variable appServer.
var server = require('../app.js');
exports.testStream = function(req,res){
var io = require('socket.io').listen(server.appServer);
io.on('connection',function(socket){
console.log("in socket");
fs.readFile('E:/temp/testimg.png',function(err,buf){
socket.emit('image',{image: true,buffer: buf});
console.log("test image");
});
})
}
when the code runs it stucks and not showing the console.logs(). What I am doing wrong over here. Any help is very much appreciated.
I would suggest following the code structure as suggested in socket.io docs.
Also, you should not be calling io.listen or io.on('connection') inside your testStream express middleware. These are things you should only be doing once, and ideally they should happen during startup, inside app.js and not in reaction to a POST request. In fact, I'm not sure what the purpose of your testStream middleware is, its not even returning any response (eg res.end())
If you want to handle socket connections in a separate module you can, but instead of exporting your app's server the way you are, try passing the io instance as variable to your submodule. In short, try this:
app.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var test = require('./test')(io);
app.set('port', process.env.PORT || 3000);
server.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
test.js
module.exports = function(io) {
io.on('connection', function(socket) {
console.log("in socket");
fs.readFile('E:/temp/testimg.png', function(err, buf) {
socket.emit('image', {
image: true,
buffer: buf
});
console.log("test image");
});
});
};
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.