Connect to Heroku socket from another server/localhost - javascript

I have a nodejs/express app deployed on heroku with socket.io listening on port 80.
I want to connect to the app's socket from localhost which will be pushed to github-pages. I'm getting net::ERR_SSL_PROTOCOL_ERROR error in the GET request.
This is my server code deployed on heroku.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
io.on('connection', function(){
});
My localhost is running on webpack-dev-server.
import io from 'socket.io-client';
const socket = io.connect('https://tank3d.herokuapp.com:80');
Everything is fine if I run the server locally.

Related

How to connect to a Node.js server that's using Express using an HTML file?

You run a node.js express server on LocalHost or a service provider (Heroku, AWS). How do I connect this to my website?
E.G -- You go to..
www.yourdomain.com -> Connects to HEROKU server -> HEROKU server sends html file
app.js
const http = require('http'),
path = require('path'),
express = require('express'),
app = express(),
serv = require('http').Server(app);
var htmlPath = path.join(__dirname, 'client');
app.use(express.static(htmlPath));
app.get('/', (req, res) => {
res.sendFile(htmlPath + '/index.html');
});
var server = serv.listen(2000, () => {
var host = 'localhost';
var port = server.address().port;
console.log(`listening on http://${host}:${port}/`);
});
In this case, now instead of going to localhost:2000 directly to receive the index.html file, you go to yourdomain.com and the server sends the index.html file
You need to set the DNS system to redirect to the public IP of the machine hosting the Express application. The express application needs to also be listening for GET requests sent to the path you are specifying.
You would also need to configure the firewall on that machine to allow port 80 and 443 to be open.

How do I implement Socket.IO in an electron app?

I want to implement Socket.IO in an Electron app, however I have found no documentation and no examples of how this could work.
If someone could explain to me how two or more clients could communicate via the electron app, I would be very grateful!
You know, the electron app will be running at end user.
So you should create Socket server at somewhere sth like Cloud server and your electron app should contain one socket.io client instance.
At Socket server
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
And at frontend (your case Electron app side)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
</script>
or
// with ES6 import
import io from 'socket.io-client';
const socket = io('http://localhost');
So that users can communicate inside your Electron app.

ERR_CONNECTION_REFUSED While Running Server on Other Laptop

I can host a server on my laptop and it will run fine, but on other computers I get the error:
ERR_CONNECTION_REFUSED
Here is the code:
var express = require('express');
var app = express();
var server = app.listen(8000);
app.use(express.static('public'));
console.log('Sever runnning');
var io = require('socket.io').listen(server);
io.sockets.on('connection', newConnection);
It works fine on my computer, but fails elsewhere.
your local server working on your machine,app is running fine but on other laptop app is not running right? so on other laptop give your network IP then see the result.

Correct ports for node server-client application with Heroku

Well, I'm near to finish my starting app... but port issue is hard...
My server.js has this
const port = process.env.PORT;
const express = require('express'), http = require('http');
const app = express();
const server = app.listen(port);
const io = require('socket.io').listen(server);
And now the issue. What should I do in the client?
var socket = io.connect('https://myweb.herokuapp.com:3000/');
var socket = io.connect('https://localhost:3000/');
I tried those two... the second one, localhost (but with 8080) works in local... but displays the error
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=M9zP_yz net::ERR_CONNECTION_RESET
I read that the client is picking the CLIENT PC localhost and that fails, that has to pick the SERVER PC route... so I tried the first option, the application URL... because I suppose I don't have to use the server IP (I also don't have it). But nothing works...
Try using port 80, that should be the default port accessible outside heroku dynos: https://myweb.herokuapp.com:80/. If it does not work, try connecting without the port. Use the URL only: https://myweb.herokuapp.com

Connecting 2 servers via Node Websockets

I have 2 Raspberry Pi running on the same Network.
I am using one as a local web server for my house, I then have another one connected to some devices. I want them to both be able to communicate to each other via web sockets but am having some problems.
My server looks like this:
express = require('express'); //web server
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server); //web socket server
server.listen(8080); //start the webserver on port 8080
app.use(express.static('public')); //tell the server that ./public/ contains the static webpages
io.sockets.on('connection', function (socket) { //gets called whenever a client connects
socket.on('Testing',function(data){
console.log("Testing connection");
});
});
My problem comes with the client connection I am really not sure what code to use to try and connect.
I have installed Express and Socket.io on my client and used this code:
console.log('1');
// Connect to server
var io = require('socket.io')
var socket = io.connect('http://192.168.1.138:8080', {reconnect: true});
console.log('2');
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
console.log('3');
But this leads to an error on the io.connect is not a function.
I am not really sure how to get the client to work so any advice is appreciated.
I should add that connecting to my webserver directly via the ip and port does load the webpages I have created successfully.
When using socket.io on the server side as a client you need to do var socket = require('socket.io-client')('http://192.168.1.138:8080', ...);. See https://www.npmjs.com/package/socket.io-client

Categories