I am having problems starting the node server.js file because the port that it wants to use is taken by Laragon Apache which allows me to open the project in the browser.
This is the node server.js file :
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(8890);
io.on('connection', function (socket) {
console.log("client connected");
var redisClient = redis.createClient();
redisClient.subscribe('message');
redisClient.on("message", function(channel, data) {
console.log("mew message add in queue "+ data['message'] + " channel");
socket.emit(channel, data);
});
socket.on('disconnect', function() {
redisClient.quit();
});
});
When I start it in cmder with node server.js is can not use the port.Node server.js command in cmder:
client connected
events.js:160
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
D:\laragon\www\canvas-messenger\nodejs (master)
node server.js
With Laragon Apache on the browser console looks like this,
Failed to load resource: net::ERR_CONNECTION_REFUSED
and
GET http://localhost:8890/socket.io/?EIO=3&transport=polling&t=1524944736161-36 net::ERR_CONNECTION_REFUSED
error continuously repeting itself because the node server.js can not run I guess.
I have tried installing redis and socket io with laravel echo server, giving them different ports that would allow them to work with out making problems to each other but the functionality of the Chat in the browser would still be lost.
Thank you for reading !
Related
I'm new to web3 and i have been trying to deploy basic hardhat setup with default Lock.sol template code to heroku and run 'npx hardhat node' with start script.
I have a nodeJs app trying to connect to the hardhat server that is deployed to the above mentioned heroku-hardhat app.
following is my code :
const Web3 = require("web3");
const web3 = new Web3("https://{myapp}.herokuapp.com");
async function getBalance(accountAddress) {
console.log("Get Account balance called", accountAddress);
console.log("web3", web3); // Web3 object is consoled properly here
const balanceInWei = await web3.eth.getBalance(
accountAddress,
(err, balance) => {
if (err) {
console.error("Error while retrieving account balance :", err); // Here, i get the error mentioned in the title
return 0;
}
console.log("Balance : ", web3.utils.fromWei(balance, "ether"), " ETH");
return balance;
}
);
I can also see the accounts and private keys listed from the logs of heroku app once "npx hardhat node" is running
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
But, the app shows the following
2022-12-11T19:11:27.462998+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-12-11T19:11:27.490453+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-12-11T19:11:27.639776+00:00 heroku[web.1]: Process exited with status 137
2022-12-11T19:11:27.708051+00:00 heroku[web.1]: State changed from starting to crashed
and the node app is unable to retrieve response, it gets the following consoled :
Error while retrieving account balance : Error: Invalid JSON RPC response: {"size":0,"timeout":0}
But, when i initialise the web3 object with the following, it works properly :
const web3 = new Web3("http://127.0.0.1:8545");
I'm basically trying to send balance of hardhat fake wallet back to frontend through nodeJs app that is unable to get JSON response from the deployed heroku-hardhat app
this error
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within
60 seconds of launch
indicates that your heroku app is not starting. this usually happens if you set your port like this:
const port = 3000;
const server = app.listen(port, () => console.log(`listening on port ${port}`));
in production, heroku assigns an environment variable for port. so you should set the port in your app like this
const port = process.env.PORT || 3000;
Since you were able to deploy the code heroku without issue, this might be the only issue in your app.
I'm currently stuck with an issue I'm getting with the coinbase-pro-node npm package (https://github.com/coinbase/coinbase-pro-node). I'm connecting to the matches channel and listening for messages there but the connection with the Websocket breaks after a few hours without telling me much. I can't trace back the problem and it doesn't happen on the same intervals. Sometimes it breaks just minutes after I run the script. Thanks for the help.
The code:
const CoinbasePro = require('coinbase-pro');
var coinbaseWs = '';
function connect() {
coinbaseWs = new CoinbasePro.WebsocketClient(
['BTC-USD'],
'wss://ws-feed.pro.coinbase.com',
{
key: 'xxxx',
secret: 'xxxx',
passphrase: 'xxxx',
},
{ channels: ['matches'] }
);
coinbaseWs.on('message', async data => {
console.log(data)
});
coinbaseWs.on('error', err => {
console.error("Connection with Coinbase websocket failed with error: " + err);
console.log("Error stack trace: " + err.stack);
});
coinbaseWs.on('close', () => {
console.error("Connection with Coinbase websocket closed!");
});
}
connect();
Error stack:
Error: read ECONNRESET
File "internal/stream_base_commons.js", line 167, in TLSWrap.onStreamRead
it does break from time to time for no apparent reason. All you can do is listen for the heartbeat messages and use those to decide whether to re-initiate a new websocket feed. I raised a similar query directly with the coinbase pro/gdax customer support.
I am using Cloud9 and am trying to run the following code:
var bodyParser = require("body-parser"),
mongoose = require("mongoose")
express = require("express"),
app = express();
mongoose.connect("mongodb://localhost/restful_blog_app");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
// title
// image
// body
// created
app.listen(process.env.PORT, process.env.IP, function(){
console.log("SERVER IS RUNNING!");
})
I go start up the server and am getting the following:
(node:2880) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
Hey don't know if you're still looking for answer on this one, but I would try restarting the Mongodb server again by using "cd" in the terminal to get to the your root directory then running "./mongod"
Hope that helps.
-
Wes jones.
when I run node serve.js, getting following error
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:6379 failed - connect ENFILE 127.0.0.1:6379 - Local (undefined:undefined)
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at connect (net.js:880:16)
at net.js:969:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
Server.js file
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(8890);
io.on('connection', function (socket) {
console.log("client connected");
var redisClient = redis.createClient();
redisClient.subscribe('message');
redisClient.on("message", function(channel, data) {
console.log("mew message add in queue "+ data['message'] + " channel");
socket.emit(channel, data);
});
socket.on('disconnect', function() {
redisClient.quit();
});
});
How can I fix above error.I have install node,npm,predis and socet.io
I had the same problem but I solved it by installing the redis and executing it previously in the terminal.
If you are using mac you can easily install redis through brew with the follow command:
brew install redis
Run the follow command after install:
redis-serve
This work's for me!
I had this problem in Laravel, the problem was I ran my socket outside of homestead/vagrant box
So SSH into homestead/vagrant navigate to your root project and run your socket script if you experience this in laravel :)
I have the current setup:
Nodejs Proxy (running http-reverse-proxy) running on port 80.
Rails server running on port 3000
Nodejs web server running on port 8888
So any request starting with /nodejs/ will be redirected to nodejs web server on 8888.
Anything else will be redirected to the rails server on port 3000.
Currently Socket.io requires a connection url for io.connect.
Note that /nodejs/socket.io/socket.io.js is valid and returns the required socket.io client js library.
However, I am not able to specify connection_url to /nodejs/ on my server.
I have tried http://myapp.com/nodejs and other variants but I am still getting a 404 error with the following url http://myapp/socket.io/1/?t=1331851089106
Is it possible to tell io.connect to prefix each connection url with /nodejs/ ?
As of Socket.io version 1, resource has been replaced by path. Use :
var socket = io('http://localhost', {path: '/nodejs/socket.io'});
See: http://blog.seafuj.com/migrating-to-socketio-1-0
you can specify resource like this:
var socket = io.connect('http://localhost', {resource: 'nodejs'});
by default resource = "socket.io"
If you are using express with nodejs:
Server side:
var io = require('socket.io')(server, {path: '/octagon/socket.io'});
then
io.on('connection', function(socket) {
console.log('a user connected, id ' + socket.id);
socket.on('disconnect', function() {
console.log('a user disconnected, id ' + socket.id);
})
})
socket.on('publish message ' + clientId, function(msg) {
console.log('got message')
})
Client side:
var socket = io('https://dev.octagon.com:8443', {path: '/octagon/socket.io'})
then
socket.emit('publish message ' + clientId, msg)
I use below approach to achieve this goal:
client side:
var socket = io.connect('http://localhost:8183/?clientId='+clientId,{"force new connection":true});
server side:
var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket) {
console.log("url"+socket.handshake.url);
clientId=socket.handshake.query.clientId;
console.log("connected clientId:"+clientId);
});
reference:https://github.com/LearnBoost/socket.io/wiki/Authorizing#global-authorization
If you are serving your app with express, then maybe you can check this out. Remember express uses http to serve your application.
const express = require('express'),
http = require('http'),
socketIo = require('socket.io'),
app = express()
var server = http.createServer(app);
var io = socketIo(server);
io.on('connection', (socket)=>{
// run your code here
})
server.listen(process.env.PORT, ()=> {
console.log('chat-app inintated succesfully')
})