Node.js + Socket.io: Unable to set ping timeout - javascript

In my Node.js + Socket.io application, I'm trying to set the ping timeout to 63 seconds (63 seconds of inactivity allowed until the connection between the server and the client(s) is considered closed). Unfortunately, when I run my code below, I get the following error:
var io = socket(server);
^
TypeError: socket is not a function
Below is my code:
var express = require("express");
var socket = require("socket.io")(3000, {
pingTimeout: 63000,
});
var app = express();
var server = app.listen(3000);
app.use(express.static("public"));
var io = socket(server);

just remove this line
var socket = require("socket.io")(3000, { pingTimeout: 63000, });
and try adding this in io variable
var io = require("socket.io")(server, {pingTimeout: 63000, });

Related

Get all connected peer(Peer.js)

How can i get all connected peers if i have simple server:
var port = 9000;
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
app.get('/', function(req,res){
res.send('server OK');
//console.log(ip.address());
});
var server = require('http').createServer(app);
app.use('/peerjs',ExpressPeerServer(server));
server.listen(port,'0.0.0.0');
and client:
var peer = new Peer(
{host:"hostname",
port:9000,path:"/peerjs",
debug:3,
config:servers,
});
i use peerjs lib. Thank in advance!
Peer.js has method listAllPeersĀ  on the client side. And you can use it if you add allow_discovery: true parameter in options on the server side.
This should help you

Node.js Socket.io socket.brodcast is undefined

brodcast.emit to send a message to all without socket, and when I do that the node instance crashes and says that socket.brodcast is undefined .
here is my node code:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('public'));
io.on('connection', function(socket){
socket.on("newChild",childData =>{
var newChildID = mainData.newChild(childData.fatherID,childData.data, childData.type);
socket.emit("newChildID",{ "newId" : newChildID,"old" : childData.localID});
socket.brodcast.emit("newChild",maindata.getDataPoint(newChildID));
});
});
when I emit "newChild" from the client the server crashes and say that socket.brodcast is undefined
The important part is to get socket.brodcast.emit , so do I use the API wrong?
when I googled after it I found this: Send response to all clients except sender (Socket.io)
In this thread I found this example:
socket.on('cursor', function(data) {
socket.broadcast.emit('msg', data);
});
And its seams to be the same as i do.
your code contains a typo for starters...
socket.brodcast.emit("newChild",maindata.getDataPoint(newChildID));
should be
socket.broadcast.emit("newChild",maindata.getDataPoint(newChildID));

WebSocket server not receiving messages immediately

The Node.JS server doesn't receive the message quickly..
Server code:
var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
var app = express()
var port = process.env.PORT || 5000
app.use(express.static(__dirname + "/"))
var server = http.createServer(app)
server.listen(port)
console.log("http server listening on %d", port)
var wss = new WebSocketServer({server: server})
console.log("websocket server created")
wss.on("connection", function(ws) {
var id = setInterval(function() {
ws.send(JSON.stringify(new Date()), function() { })
}, 1000)
console.log("websocket connection open")
ws.on("message", function(msg){
ws.send(msg);
})
ws.on("close", function() {
console.log("websocket connection close")
clearInterval(id)
})
})
Code to connect:
var z = new WebSocket("ws://appname.herokuapp.com/");
z.onopen = function(){ console.log("opened"); };
// ... after opened message seen in console, the following
// were typed directly into the console, a few seconds apart
z.send("H");
z.send("Hello World");
z.send("Hello World asdf");
Until the third send was done (even if it was a minute after the others), the server did not respond with H or Hello World; all three responses were made at the same time. How do I make the server respond in a timely manner?
(Using WireShark, I checked that the sends were actually done; they were)
Edit: The amount of requests/time before response varies each time.

socket.io connection not heppening

This is first time, I am using socket.io.I stuck at initial stage itself.sorry it's may be simple question.
server side code :
Inside my server.js I written the following code.
var express = require('express')
,io=require('socket.io')
,http = require('http')
var app = express();
server = http.createServer(app);
io = io.listen(server,{ log: false });
Now I trying to make connection inside server.js file,like in the following way.
io.sockets.on('connection', function (socket) {
console.log("This is testing");
io.to(socket.id).emit('notification', 'for your eyes only');
});
client side code :
var socket = io.connect("http://localhost");
socket.on('connect', function () {
console.log("connect")
});
socket.on('notification', function (data) {
console.log(data);
});
I open application in browser, as per my code it suppose to console connect statement but it's not happening.
my server is running on port no :80Where am I did wrong, can anyone help me.
Thanks.
Here is the working code for me in express it may help you.
var express = require('express')
, app = express()
, server = require('http').Server(app)
, io = require('socket.io')(server)
var defaultPort = 6001 ;
server.listen(defaultPort, function() {
console.log('Server Started');
});
io.sockets.once('connection', function(socket) {
return io.sockets.emit('new-data', {
channel: 'stdout',
value: "Your Data Goes Here"
});
socket.on('disconnect', function(){
});
});
On Client Side
<script>
$(function() {
var socket = io.connect('http://localhost'); //if you are trying on server put server url if you are working on local then use localhost
socket.on('new-data', function(data) {
$('#YouDivid').html(data.value);
});
});
</script>

Socket.io 400 (Bad Request)

I have this piece of code on my server
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
io.sockets.on('connection', function (socket) {
console.log("Socket connected");
});
I just want to create a connection
and on the client -
<script src="public/javascripts/socket.io.js"></script>
<script>
var socket = io.connect('http://127.0.0.1:3000');
</script>
And when I open my browser I get this error in console:
GET http://127.0.0.1:3000/socket.io/1/?t=1404410309733 400 (Bad Request) socket.io.js:1659
XHR finished loading: GET "http://127.0.0.1:3000/socket.io/1/?t=1404410309733".
I've already done this like 10 times and I never get this. Does anyone know how to fix it?
I think an older version of socket-io could be the reason.
I was getting this problem when I was using 0.9.16 version. I upgraded to following and it worked.
<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>

Categories

Resources