Node js - Socket.io - client is not connecting to socket.io server - javascript

I am trying to connect to a socket.io-client using this as my server.js:
var express = require('express');
var app = express();
var server = app.listen(3001);
var io = require('socket.io').listen(server);
app.get('/input', function(req, res){ // This is because in the server the port 3001 is open and I only can use the route 'input' to connect to the node server
res.send('Hello world');
});
io.on('connection', function(socket) {
console.log('Client connected.');
// Disconnect listener
socket.on('disconnect', function() {
console.log('Client disconnected.');
});
});
And in my client file:
var socket = io.connect('https://urlofthepage:3001', {reconnect: true});
socket.on('connect', function() { console.log('connect'); });
// or (I tried with both)
var socket = io.connect('https://urlofthepage:3001/input', {reconnect: true});
socket.on('connect', function() { console.log('connect'); });
But when I go to urlofthepage/input show me
Hello world
but in the node server not show anything like
Client connected.
And in the page where I have the client.js file the console show me
3001/socket.io/?EIO=3&transport=polling&t=MBSSeZj net::ERR_CONNECTION_TIMED_OUT
Edit: It's on an online server, which has a wordpress installed, and my socket.io.js script is urlofpage/socket.io/socket.io.js
AND (I don't know if this matters, it is a test server, and the url has https, but it indicates that it is not secure, we have to change it)

Related

socket.io is not working with static file routing node.js

I'm writing a chat application. In that, when the static file routing is working the socket.io (Chat) is not working throws not found error in console.
http://localhost/socket.io/?EIO=3&transport=polling&t=1486739955177-8 404 not found
When the chat is working fine then public static files is not working throws error
Cannot GET /public/index.html
The code chat working (public static files not working) :
var app=require('express')();
var http=require('http').Server(app);
var io=require('socket.io')(http);
var path=require('path');
//Initialize application with route
app.get('/',function (req,res) {
var express=require('express');
app.use(express.static(path.join(__dirname+'/public')));
res.sendFile(path.join(__dirname,'../public','chat.html'));
});
//Register events on socket connection
io.on('connection',function (socket) {
socket.on('chatMessage',function (from, msg) {
io.emit('chatMessage',from,msg);
});
socket.on('notifyUser',function (user) {
io.emit('notifyUser',user);
});
});
// Listen appliaction request on port 80
http.listen(80,function () {
console.log('Server Running in port 80');
});
The code public static files working ( chat not working) :
var app=require('express')();
var http=require('http').Server(app);
var io=require('socket.io')(http);
var path=require('path');
//Initialize application with route
var express=require('express');
app.use(express.static('public/'));
app.use('/public',express.static('public/stack'));
//Register events on socket connection
io.on('connection',function (socket) {
socket.on('chatMessage',function (from, msg) {
io.emit('chatMessage',from,msg);
});
socket.on('notifyUser',function (user) {
io.emit('notifyUser',user);
});
});
app.get('*', function(req, res){
res.send('what???', 404);
});
// Listen appliaction request on port 80
app.listen(80,function () {
console.log('Server Running in port 80');
}
Ok this code works
var express=require('express');
var app = express();
var path=require('path');
var server = require('http').createServer(app);
var io=require('socket.io')(server);
//Initialize application with route
app.use(express.static('public/'));
// app.use('/public',express.static('public/stack'));
//Register events on socket connection
io.on('connection',function (socket) {
socket.on('chatMessage',function (from, msg) {
io.emit('chatMessage',from,msg);
});
socket.on('notifyUser',function (user) {
io.emit('notifyUser',user);
});
});
app.get('/', function(req, res){
res.send('what???', 404);
});
// Listen appliaction request on port 80
server.listen(80,function () {
console.log('Server Running in port 80');
});
Move your chat.html in side public folder and access like http://localhost/client.html
Directory structure is like
appdir
public/client.html
server.js
node server.js

Socket.IO Client How to Connect?

I was following the second example here:
https://github.com/socketio/socket.io-client
and trying to connect to a website that uses websockets, using socket.io-client.js in node.
My code is as follows:
var socket = require('socket.io-client')('ws://ws.website.com/socket.io/?EIO=3&transport=websocket');
socket.on('connect', function() {
console.log("Successfully connected!");
});
Unfortunately, nothing gets logged.
I also tried:
var socket = require('socket.io-client')('http://website.com/');
socket.on('connect', function() {
console.log("Successfully connected!");
});
but nothing.
Please tell me what I'm doing wrong. Thank you!
Although the code posted above should work another way to connect to a socket.io server is to call the connect() method on the client.
Socket.io Client
const io = require('socket.io-client');
const socket = io.connect('http://website.com');
socket.on('connect', () => {
console.log('Successfully connected!');
});
Socket.io Server w/ Express
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const port = process.env.PORT || 1337;
server.listen(port, () => {
console.log(`Listening on ${port}`);
});
io.on('connection', (socket) => {
// add handlers for socket events
});
Edit
Added Socket.io server code example.

WebSocket connection closes when server sends message only on port 80

I have a simple, local Nodejs server running Express. I'm using express-ws to set up a WebSocket endpoint. The client sends messages fine, and the server receives them, but when the server tries to send a message back, the connection closes and the client never receives the message.
This only happens over port 80. The connection stays open over port 3000, 8080, 443, and the client receives the messages the server sends back.
app.js
const express = require('express');
const path = require('path');
const app = express();
const expressWs = require('express-ws')(app);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res, next){
res.send(`<script src="js/client.js"></script>`);
});
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log(msg);
ws.send(msg); //The connection doesn't close with this commented out
});
});
app.listen(80);
client.js
const ws = new WebSocket(`ws://${window.location.host}`);
ws.onopen = function(e){
console.log("WebSocket connected");
ws.send("testing 1");
ws.send("testing 2");
}
ws.onmessage = function(msg){
console.log(msg);
}
I'm at a loss. Any help would be appreciated.

Express nodejs socket.io with cordova

I'm trying to implement socket.io on my server. This server is an API (express nodejs).
The server side is simple, but for the client side I'm using phonegap/cordova.
I don't use a phone to test what I do, I use my browser (chrome).
Si this the server side :
var express = require('express'); // call express
var app = express(); // define our app using express
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
console.log('a user connected');
console.log(socket);
socket.on('disconnect', function () {
console.log('socket disconnected');
});
io.emit('text', 'wow. such event. very real time.');
});
for now, this is simple,
But for the client side I am completely confuse (cordova phonegap),
This is what I have :
index.html
<script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('news', function (data) {
console.log('send')
socket.emit('my other event', { my: 'data' });
});
</script>
Nothing appears but errors like
GET http://localhost:8080/socket.io/?EIO=2&transport=polling&t=1462638049681-3 net::ERR_CONNECTION_REFUSED
and nothing on my server
any ideas to help me ? thanks :)
Your server is not listening on port 8080. That's why when you try to connect from browser to var socket = io.connect('http://localhost:8080');, it shows 'Connection Refused'.
This edit would work for you.
var express = require('express'); // call express
var app = express(); // define our app using express
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
console.log('a user connected');
console.log(socket);
socket.on('disconnect', function () {
console.log('socket disconnected');
});
io.emit('text', 'wow. such event. very real time.');
});
//Now server would listen on port 8080 for new connection
http.listen(8080, function(){
console.log('listening on *:8080');
});

getting started express.io first simplest example

I'm trying this simple example:
Server:
app = require('express.io')()
app.http().io()
app.io.on('connection', function(socket){
console.log('connection')
})
app.listen(50000)
Client:
var io = require('socket.io-client').connect('http://localhost:50000')
io.on('connect', function (sock) {
console.log("socket connected")
})
Neither 'connection' nor 'socket connected' appears in nodejs console
However separate express and socket.io server works like a charm:
var app = require('express')()
var http = require('http').Server(app)
var io = require('socket.io')(http)
io.on('connection', function(socket){
console.log('connection')
})
http.listen(50000)
I'd like to get it done with express.io for educational purposes, but there is no forum, no archives, ...nowhere to ask

Categories