This is the code of the Server:
var http = require('http');
var app = require('express')();
http = http.createServer(app).listen(3400,() => {
var io = require('socket.io')(http);
io.on('connection', (socket) => {
console.log('connection');
});
});
I am using the Socket.io Client Test Tool: https://amritb.github.io/socketio-client-tool/
When I enter the address http://myWANIP:3400 as the URL and hit connect, nothing happens.
What am I doing wrong?
Any help is much appreciated :D
Edit: The Host is behind a NAT and the port 3400 is being forwarded (TCP).
Edit2: After some helpful comments I changed the code to:
var http = require('http');
var app = require('express')();
var server = http.createServer(app).listen(3400);
var io = require('socket.io')(server);
io.on('connection', (socket) => {
console.log('connection');
});
Your second code block looks more appropriate. We can't really tell if your NAT and port forwarding is set up correctly, but if it is, then you should be able to make a socket.io connection from a web page with this:
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io("http://myWANIP:3400", {transports: ["webSocket"]});
</script>
And, when that connection occurs, you should see the results of console.log('connection'); in the server logs.
Another way to verify that your NAT and port forwarding is working correctly is to add this to your server:
app.get("/", (req, res) => {
console.log("got web page request");
res.send("hello");
});
Then, when you got to http://myWANIP:3400, in the browser, you should get a log on your server and a response page back that says "hello".
Related
How can I setup socket in client at my page with url
/localhost:3000/post/:id
In client I set
socket=io("http://localhost"3000/post/:id")
but when I listen in server with io.on("connection") , it isn't active.
In the client side you just need to include the socket.io cdn file and when you are on same host you don't have to include the full url.
code sample for client side :
let socket = io.connect();
socket.emit("connection",{});
For better understanding please view documentation as said on previous comment.
This is how you should configure it :
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
io.on('connection', (socket) => {
console.log('a user connected');
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
Read this documentation to understand it better https://socket.io/docs/v3
I am using ASP.NET CORE 2.0 to build an e-commerce. The e-commerce has a chat built using nodejs and the package socket.io. The socket.io server is remote on the server. When I use the Socket.io client locally, running Visual Studio Debugger, to access the remote socket.io, all works fine.
The code is like this, note that I am not using https
var app2 = require('express')();
var http = require('http').Server(app);
var http2 = require('http').Server(app2);
var io = require('socket.io')(http);
http.listen(3009, function () {
console.log('listening on port 3009');
});
http2.listen(3011, function () {
console.log('listening on port 3011');
});
But when I publish my web site and get the html page along with the socket.io client served by Nginx/kestrel I got an error message saying that I was mixing something, I didn't pay attention to the error message because I remembered that I was using http on my server socket.io and clients. So I changed the socket.io server and clients but now I cannot connect.
My changes are like this:
var app2 = require('express')();
var http = require('https').Server(app);
var http2 = require('https').Server(app2);
var io = require('socket.io')(http);
http.listen(3009, function () {
console.log('listening on port 3009');
});
http2.listen(3011, function () {
console.log('listening on port 3011');
});
clients
myIo = io('https://www.example.com.br:3009', { secure: true, reconnect: true, rejectUnauthorized: false });
I used Let's encrypt to enable https connections, I am using Nginx as proxy for Kestrel, I am using ufw on Ubuntu 17.
I got this error yesterday. I couln't even sleep at night. But I got it working. I sent the certificates like this.
var app = require('express')();
var app2 = require('express')();
var fs = require('fs');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};
var http = require('https').Server(options, app);
var io = require('socket.io')(http);
app.get('/', function (req, res) {
res.send('server is running');
});
app2.get('/', function (req, res) {
res.send('admin area');
});
I don't want anyone passing the frustration felt. Hope I can help somebody.
You need to add the transports type for your client and server
server
var io = require('socket.io')(http);
io.set('transports', ['websocket']);
client
myIo = io('https://www.example.com.br:3009', { transports:
['websocket'], upgrade: false }, { 'force new connection': true });
I tried to setup socket.io into my node server in order to make a messaging function for my app but having problem getting to function despite doing test app correct.
server.js:
/* long list of requires */
var express = require('express');
var http = require('http');
var https = require('https');
var config = require('./config');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('a user connected');
});
/* ssl setup */
// Create an HTTP service.
http.createServer(app).listen(8000); // opens the port
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(4433); // for the ssl
index.html
..bunch of code like bootstrap and angular.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8000');
socket.on('connect', function(data) {
socket.emit('join', 'Hello World from client');
});
</script>
I tried to run the site on localhost:8000.
Despite all that, I'm getting error 404 in the console:
GET /socket.io/?EIO=3&transport=polling&t=LnM0Fd3 404
I'm not sure what went wrong with the set up process of socket.io
You're creating three servers, only one of which gets the Socket.io
// createServer No 1
var server = require('http').createServer(app);
var io = require('socket.io')(server);
// ...
// createServer No 2
http.createServer(app).listen(8000); // opens the port
// Create an HTTPS service identical to the HTTP service.
// createServer No 3
https.createServer(options, app).listen(4433); // for the ssl
You should probably only create one server and add the socket.io call to that one.
If you create only the Port 8000 server, you could add it like this:
// (exactly your code, except for the "var server = " in front.
var server = http.createServer(app).listen(8000); // opens the port
// the next line is new:
var io = require('socket.io')(server);
notice that you'll also have to move the io.on...... part below that line.
i am using node.js and socket.io for the real time notification system, so i have tested node.js and socket.io with simple chat code, it pretty good with localhost but can't access the same from the another system which are connected locally with same network, my server and client code looks like below
server.js
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
server.listen(8888);
and client html index.html
<script src="http://localhost:8888/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect('http://localhost:8888');
</script>
it's working on my browser with this url http://localhost/schat/index.html but not working when i'm trying connect from another one system using my ip 192.171.56.23/schat/index.html but all other html files working fine, below is my netstat output
[root#localhost schat]# netstat -pan | grep 8888
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 8068/node
tcp 0 0 127.0.0.1:8888 127.0.0.1:38273 ESTABLISHED 8068/node
tcp 0 0 127.0.0.1:38273 127.0.0.1:8888 ESTABLISHED 7990/firefox
Here is a working example from socket.io docs (slightly modified).
use <script src="/socket.io/socket.io.js"></script>
instead of <script src="http://localhost:8888/socket.io/socket.io.js"></script>
and use var socket = io.connect(window.location.origin);
instead of var socket = io.connect('http://localhost:8888');
(socket.io v1.3.5, express v4.12.2)
index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect(window.location.origin);
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
server.js
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(8888);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Change your client side code to
<script>
var url = window.location.host;
var socket = io.connect(url);
</script>
I had the same problem and I fixed it :
First you have to change http://localhost:8888/ to http://your_IP_address:8888/ everywhere in your code.
Second, which took me too much time to fix was to change server.listen(8888); to server.listen(8888, "your IP address");
Thank Karim, your solution work for me, in the 'server.js' configuration I set the 'port' value and the 'server', but not the ip, I use only the name without 'https://', like this:
// External route.
var express = require("express");
var router = express.Router();
// In cloud 9 use port 8081
const port = 8081;
const webLnk = 'mywebdevaddress.io';
// In dev local ambient 'localhost' work without the 'port' and the server, in prod internet // you need to specify the port and the link name.
const server = require('http').Server(router).listen(port, webLnk);
const io = require('socket.io')(server);
Is the official socket.io docs I do not see any of this!
I have changed my files to look as shown below. The javascript console shows nothing and the web page shows nothing. Just a blank screen. Does anybody know of a web site which uses socket.io. I would like to inspect the code to see how they do it since none of the examples on the socket.io page work for me. Does anybody know if extra ports need to be allowed in the iptables file?
Using the Chrome browser, if I goto the javascript console and goto the Network tab I get successes but the last call says Pending ??? It looks like it is hung. Could this be a firewall issue?
/socket.io/1/websocket
GET
101
Switching Protocols
Pending
Other
127 B
0 B
Pending
it is Pending in selector.js on line 168:
document.body.appendChild(menuDiv);
Maybe this is why I am not seeing anything? It is properly serving up socket.io/socket.io.js
Files below:
//app.js
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
app.use(express.static(__dirname + '/public'));
var port = 80;
var io = require('socket.io').listen(server, {
log : false
});
io.sockets.on('connection', function(socket) {
socket.on('hi', function(data) {
console.log('yay');
socket.emit('hello');
});
});
server.listen(port);
public/index.html
//index.html
<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
var chat = io.connect('http://localhost/chat')
, news = io.connect('http://localhost/news');
chat.on('connect', function () {
chat.emit('hi');
});
chat.on('hello',function(){
console.log('yay got hello');
});
var socket = io.connect('http://localhost:80');
socket.on('connect', function () {
chat.emit('hi');
});
socket.on('hello',function(){
console.log('yay got hello');
});
</script>
You're using namespaces news and chat even though your server doesn't implement them correctly.
You javascript connection should look like this instead:
var socket = io.connect('http://localhost:80');
socket.on('connect', function () {
chat.emit('hi');
});
socket.on('hello',function(){
console.log('yay got hello');
});