Why socketIO call too much connections? - javascript

I created my Socket server with Express, SocketIO and Redis
server.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(8890, function (e) {
console.log(e);
});
io.on('connection', function (socket) {
console.log("new client connected");
var redisClient = redis.createClient();
redisClient.subscribe('message');
redisClient.on("message", function(channel, message) {
console.log("mew message in queue "+ message + "channel");
socket.emit(channel, message);
});
socket.on('disconnect', function() {
redisClient.quit();
});
socket.on('connect_error', function() {
redisClient.quit();
});
});
From command line, I run node server.js. Its worked.
I also created a html file to connect to that server.
From Browser Console, I run io.connect('http://localhost:8890'). I got as the result
As I see, too much connections (requests).
What happens? What wrong from my code?

You have mismatched client and server versions causing the initial connection to fail and the older client is dumb enough to just keep trying over and over again. If you are using 2.0.4 on the server, then you must use that version for the client too. If you serve the client version directly from your server with:
<script src="http://localhost:8890/socket.io/socket.io.js"></script>
Then, the socket.io server will automatically give you the right client version.
Or, you can manually link to the right version on your CDN such as https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js. But client and server versions MUST match.
The advantage of getting the client directly from your own server is that anytime you update your server version of socket.io, the client version will automatically be upgraded for you and kept in perfect sync since the matching client version is built into the server version.

Related

Can't connect to socket.io server from Node JS script

I've got a socket.io server running on Node JS listening for messages. I now need to start another Node JS script (my client) which will send messages to the server, that's at a specific URL and port.
I'm passing my URL into the connect function but am getting the error:
TypeError: io.connect is not a function
How can I pass my URL to connect to into this. My script is:
const io = require('socket.io');
const socket = io.connect('http://localhost:3000');
socket.on('connect', function () {
socket.send('hello') // send to server from running JS script
});
If you're attempting to initiate a socket.io connection from node.js, you need the client-side library which would be:
const io = require('socket.io-client')
And, of course, you have to install that library.

Can socket.io connect to ws://?

Right now I am setting the nodejs server to use webscoket to receive the data from other server
I have tried to use websocket(ws) in nodejs server, and it can connect to the other server
const WebSocket = require('ws');
var ws = new WebSocketClient();
ws.open("ws://ws.something/?token="+Token);
But when I try to connect using socket.io, the console does not display a debug message, and no data is received
var io = require('socket.io-client')
const socket = io.connect('ws://ws.something/?token='+Token);
socket.on("connection", function(mSocket){
console.log('debug message')
mSocket.on("message", function(myData){
console.log(myData)
});
});
socket.on('error', function (err) {
console.log(err);
});
Can socket.io connect to ws:// ? Or is there something wrong with my code? (This is the first time to use it)
No, it cannot.
Socket.IO is a layer on top of several transports, with Web Sockets being only one of them. Socket.IO clients can only connect to Socket.IO servers.
If you want to connect to your Web Socket server from a browser, use the browser's built-in Web Socket client:
const socket = new WebSocket('wss://example.com');
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

Problems using socket.io, socket.io.js not found

I receive this error in console GET http://localhost/socket.io/socket.io.js 404 (Not Found). I used npm install to install express and socket.io. Everytime I try to access localhost:3000 it downloads a file instead of displaying chat.php
This is my javascript code
var express = require('express')
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
server.listen(3000);
users = [];
connnection = [];
console.log('Server running!');
app.get('/',function(req, res){
res.sendFile(__dirname + '/game.php');
});
io.sockets.on('connection', function(socket){
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
//Disconnect
socket.on('disconnect', function(data){
connections.splice(connections.indexOf(socket),1);
console.log('Disconnected: %s sockets connected', connections.length);
});
});
And this is what I added into php file
<script src="/socket.io/socket.io.js"></script>
<script>
$(function(){
var socket=io.connect();
});
</script>
res.sendFile(__dirname + '/game.php'); just sends a raw PHP file to the browser client. Instead, what you need to send to the browser is HTML. So, you either have to change your app to run in node.js and not PHP or you have to exec that PHP file and grab its output and send that to the browser.
Normally, if you wanted your page to be generated via PHP, you wouldn't be using node.js at all - you'd just be using PHP. If the only reason you brought node.js into the equation is because of socket.io, then maybe you should be using socket.io directly with PHP which you can read about in this question. You could use a hybrid of node.js and PHP, but it's unlikely to be all that efficient if you're using node.js to run your PHP. For that case, you would probably be better off running socket.io in node.js on a different port number, enabling cross origin access and just leaving your PHP to be PHP.
I found the working solution with PHP here https://github.com/jdutheil/nodePHP. I tested it and it is working just great.

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

Socket.io initialization resulting in a 404 network error

I am using nodejs and express to create a basic chat app and I'm getting a network 404 error message when trying to initialize the socket.io object.
<script src="/javascripts/socket.io/socket.io.js-client"></script>
<script type="text/javascript">
var socket = io.connect();
</script>
Tha above code results in a 404 error for some polling call
"NetworkError" 404 Not Found - http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1432851505880-89
I need the polling to run using the following url instead: http://localhost:3000/javascripts/socket.io/?EIO=3&transport=polling&t=1432851505880-89
because I am using express, but not sure how to accomplish this.
Server Side Code:
var app = express();
var server = require( "http" ).createServer( app );
var io = require("socket.io").listen(server);
server.listen(8888);
io.sockets.on('connection', function(socket){
socket.on('send message', function(data){
io.sockets.emit('new message', data);
});
});
module.exports = app;
I have been trying to troubleshoot this one for quite a while now with no success. I appreciate any advice. Commenting out the var socket = io.connect() resolves the error. Appreciate any advice.
Thanks
Your web page is apparently running on port 3000, but your socket.io server is listening on port 8888. The two must be the same port so it is no surprise that there is no response for a socket.io request on port 3000 (since your socket.io server is listening on port 8888).
Because the default URL it is trying is port 3000, then that must be the port that your web page is one and it must be served by a different web server. If that is the case, then you will need to do one of two things:
Combine the web server that serves your web pages with the socket.io server so the same server is taking care of both.
Specify the port in the client request and enable your socket.io server for cross-origin requests.
To specify the port in the client request, you can do this:
<script src="http://myserver.com:8888/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io("http://yourserver.com:8888");
</script>
Note, that this is requesting the client socket.io library from the express server where your socket.io server is (so it's using the same port that your socket.io server is running on).
If the HTML where socket.io client javascript is being served is not also being served by express at port 8888 (it looks like the html is coming from port 3000), you may just need to configure the socket.io client to point at the port where the server-side socket.io has been setup to listen (8888):
var socket = io.connect('localhost:8888');

Categories