Get all connected peer(Peer.js) - javascript

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

Related

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

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, });

How to make the websocket server be at a router?

I'm writing a websocket server using nodejs-ws module, but the server can only be at the root of the server, so how I can make it at a child router like localhost:3000/chat?
I need your help, thanks a lot!
Working example:
var ws = require('ws');
var http = require('http');
var httpServer = http.createServer();
httpServer.listen(3000, 'localhost');
var ws1 = new ws.Server({server:httpServer, path:"/chat"});
ws1.on('connection', function(){
console.log("connection on /chat");
});
var ws2 = new ws.Server({server:httpServer, path:"/notifications"});
ws2.on('connection', function(){
console.log("connection on /notifications");
});
could you please tell me how to use this in express?
To route websockets with Express I'd rather use express-ws-routes
var express = require('express');
var app = require('express-ws-routes')();
app.websocket('/myurl', function(info, cb, next) {
console.log(
'ws req from %s using origin %s',
info.req.originalUrl || info.req.url,
info.origin
);
cb(function(socket) {
socket.send('connected!');
});
});

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));

Switch from node-static to express

I have a simple application running on node.js with websockets. This application uses the node-static module for serving some html pages with css, js and so on.
The folder structure is this:
-app
- index.html
- server.js
- img/
- base.png
- sub/
- sub.png
- scripts
- base.js
- sub/
- sub.js
- css
- base.css
- sub/
- sub.css
Where server.js is the server file. Inside server.js there is the following code:
var static = require('node-static');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function (req, res) {
file.serve(req, res);
}).listen(process.env.PORT || 1234);
var WebSocketServer = require('websocket').server;
new WebSocketServer({
httpServer: app,
autoAcceptConnections: false
}).on('request', onRequest);
...
Now I need to switch from node-static to Express because I need to use routes. I used this code, however it doesn't work:
var express = require('express');
var app = express();
var http = require('http');
var httpServer = http.Server(app);
app.use(express.static(__dirname+'/app'));
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
app.listen(1234);
var WebSocketServer = require('websocket').server;
new WebSocketServer({
httpServer: app,
autoAcceptConnections: false
}).on('request', onRequest);
...
I can serve files, however it breaks the websocket connection.
What's wrong? Please note that the solution should be suitable for working on localhost and Heroku.
I resolved my problem using the following code:
var http = require("http");
var express = require("express");
var app = express();
var port = process.env.PORT || 1234;
app.use(express.static(__dirname + "/"));
var server = http.createServer(app);
server.listen(port);
console.log("http server listening on %d", port);
var WebSocketServer = require('websocket').server;
new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
}).on('request', onRequest);
...
This code is derived from https://devcenter.heroku.com/articles/node-websockets, however my code use the 'websocket' node module instead of 'ws'. Thanks to #Tony for the hint.

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