How do I get the IDs of connected peers? - node.js

I managed to get the number of peers connected in my console log by having the console print out "You are peer number n" for each new connected peer.
Is there a way of adding the id of each connected peer too?
I'm relatively new with socket.io and I can't seem to find a way to do this.
This is my server.js file:
var app = require("express")();
var express = require("express");
var server = require("http").Server(app);
var p2pserver = require("socket.io-p2p-server").Server;
var io = require("socket.io")(server);
var ss = require("socket.io-stream");
var path = require("path");
app.use(express.static(__dirname));
io.use(p2pserver);
var peerNum = 0;
io.on("connection", function(socket) {
console.log("Peer " + peerNum + " connected");
io.emit("peer-num", peerNum);
peerNum++;
ss(socket).on("file", function(stream, data) {
var filename = path.basename(data.name);
var parts = [];
stream.on("data", function(data) {
parts.push(data);
});
stream.on("end", function() {
socket.broadcast.emit("file", parts);
});
});
});
server.listen(8080, function () {
console.log("Listening on 8080")
});
This is my client.js file:
// Browserified
var P2P = require("socket.io-p2p");
var io = require("socket.io-client");
var ss = require("socket.io-stream");
var socket = io();
var opts = {autoUpgrade: true, peerOpts: {numClients: 10}};
var p2p = new P2P(socket, opts);
var $ = require("jquery");
p2p.on("peer-num", function(num) {
console.log("You are peer number " + num);
$(".peerNum").html( "Number " + num );
});
p2p.on("file", function(stream) {
console.log(stream);
var img = document.createElement("img");
img.src = (window.URL || window.webkitURL).createObjectURL(new Blob(stream));
document.getElementById("receivedImages").appendChild(img);
});
$(function() {
$("#file").change(function(e) {
ss.forceBase64 = true;
var file = e.target.files[0];
var stream = ss.createStream();
ss(socket).emit("file", stream, {size: file.size,name:file.name});
var blobStream = ss.createBlobReadStream(file);
var size = 0;
blobStream.on("data", function(chunk) {
size += chunk.length;
console.log(Math.floor(size / file.size * 100) + "%");
});
blobStream.pipe(stream);
});
});

If what you mean by id is the internal id that socket.io assigns to a connection (on the server), then you can access that with socket.id. So, if you want to just use socket.id instead of your peerNum, variable, then you would change this:
io.emit("peer-num", peerNum);
to this:
io.emit("peer-num", socket.id);
The socket.id value is used only on the server. It is not meant to be a human readable moniker (it contains randomly generated characters), but it can be used to reference any given connection. Since its a string, it can be easily sent to other clients who can then use it when sending messages back to the server to reference particular client connections.

Related

Node Server not working with SSL

I have a node server code on named server.js
var env = require('dotenv').config({path: '../.env'});
var fs = require("fs");
var options = {
key: fs.readFileSync(env.APP_SSL_PATH + 'server.key').toString(),
cert: fs.readFileSync(env.APP_SSL_PATH + 'server.crt').toString()
};
var app = require('express')();
var http = require('https');
var server = http.createServer(options, app);
var io = require('socket.io')(server);
var redis = require('redis');
// Check if server is listening on its port
server.listen(env.APP_NODE_PORT, function() {
console.log('Listening on your port');
io.on('connection', function (socket) {
console.log("new client connected");
var redisClient = redis.createClient();
redisClient.subscribe('notification-channel', function(err, count) {
});
redisClient.on("message", function(channel, message) {
console.log( " message= "+ message + " channel = " + channel);
socket.emit(channel, message);
});
socket.on('disconnect', function() {
redisClient.quit();
});
});
});
Using http it works fine but then when I add the SSL certificate on https the code is not working already and it doesn't throw any error. I run the node server.js by
node server.js > stdout.txt 2> stderr.txt
there are no error log showing on stderr.txt file. Please help me Im new on this. On the console
Listening on your port
will logged only it does not continue and log
new client connected
This is my client side code:
<script>
var socket = io.connect('{{ env("APP_URL") }}:8890');
socket.on('notification-channel', function (data) {
var header_count_elem = $('#menubar #header-messages-count');
var count = header_count_elem.text();
if (data != 'no-sound') {
$.get('/notification/notif-alerts', function(response) {
header_count_elem.text((response.count == '0') ? '' : response.count);
if (data != 'no-sound') {
if (count != response.count) {
var sound = '<audio id="audioplayer" autoplay=true volume=1>' +
'<source src="{{ url('assets/themes/cpanel/alarms/notification_sound.mp3') }}" type="audio/mpeg">' +
'<source src="{{ url('assets/themes/cpanel/alarms/notification_sound.ogg')}}" type="audio/ogg">' +
'</audio>';
$('body').append(sound);
}
}
});
}
});

Socket.io events working only in the same process

I'm trying to use socket.io & sticky-session to pass messages to my clients.
The problem is that client which connect to one of the processes won't get messages from other processes, only from the process he is connected to.
How can I make web sockets to work across all processes?
Server.js:
var cluster = require('cluster');
var app = require('./config/express')(db);
// Init the server to run according to server CPU's
if (cluster.isMaster) {
for (var i = 0; i < 4; i++) {
cluster.fork();
}
} else {
app.listen(config.port, function () {
console.log('Process ' + process.pid + ' is listening to all incoming requests');
});
}
Process.js:
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var ns = io.of('/ns');
var sticky = require('sticky-session');
if (!sticky.listen(server, 8080)) {
// Master code
server.once('listening', function() {
console.log('server started on 8080 port');
});
}
client.js:
var io = require('socket.io-client');
var serverUrl = 'http://localhost:8080/ns';
var conn = io.connect(serverUrl);
conn.on('malware', function(infectedProcess){
console.log('infectedProcess: ' + infectedProcess);
});

Socket.io delay in emit/receive when more than 80 concurrent connections are happening

I have a very simple example.
Server
var WebSocketServer = require('socket.io');
var wsServer = WebSocketServer.listen(8080);
wsServer.on('connection', function (ws) {
ws.on('data', function (data) {
console.log(data);
});
});
Client
var WebSocketServer = require('socket.io-client');
var wsClient = WebSocketServer.connect(server, {'force new connection': true});
wsClient.on('connect', function () {
wsClient.emit('data', {message: true});
})
I then instantiate concurrent calls:
var ClientSocket = require('./path-to-client');
var numInstances = 100;
for (var i = 0; i < numInstances; i++) {
(function (client_id) {
var WavegetInstant = new ClientSocket();
})("client" + i);
}
This works fine when numInstances is less than 80. But when it goes over 80, then there is about 2min of delay before messages are transmitted/received. If I console.log right in wsServer.on('connection'), then I do get a connection and a unique web socket id, its just the on('data') OR emit('data') being delayed.
Help is appreciated

How to set RedisStore -- Node, Express, Socket.io, Heroku

I'm using Node & Express 4.0 deployed on Heroku, and I'm trying to implement Socket.io with Redis as aa session store. So I have this as my current code:
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var RedisStore = io.RedisStore;
if (process.env.REDISTOGO_URL) {
// inside if statement
var rtg = require("url").parse(process.env.REDISTOGO_URL);
var redis = require("redis").createClient(rtg.port, rtg.hostname);
redis.auth(rtg.auth.split(":")[1]);
} else {
var redis = require("redis").createClient();
}
/** Initialize RedisStore for socket.io **/
io.set('store', new RedisStore({
redis : redis
}));
But I get the following error:
14:25:03 web.1 | io.set('store', new RedisStore({
14:25:03 web.1 | ^
14:25:03 web.1 | TypeError: undefined is not a function
I've also seen this way of defining a RedisStore:
var redis = require('socket.io/node_modules/redis');
var RedisStore = require('socket.io/lib/stores/redis');
However, my installed version of socket.io, installed using npm install --save socket.io, doesn't include stores in the lib directory:
EDIT
I saw this on the socket.io page in regards to their 1.0 release:
// 2. Implement the socket.io-redis adapter
var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
But there's no other documentation I could find regarding this new module, and since I'm new to this whole stack, I don't think I could figure it out on my own.
The trend among node.js modules is to remove functionality that isn't truly core to the module.
Which is why socket.io 1.0 no longer supports redis out of the box.
So step one is to track down the functionality you need.
http://socket.io/docs/server-api/
https://github.com/Automattic/socket.io-adapter
https://github.com/Automattic/socket.io-redis
Then you need to install the other module npm install socket.io-redis --save
And finally configure your app.
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var redis = require('socket.io-redis');
io.adapter(redis(process.env.REDISTOGO_URL));
The nice part is the socket.io-redis adapter accepts redis urls and defaults to localhost:6379 so you (should) be able to simple pass in the REDISTOGO_URL
I had to parse libraries above to get this example, so I figured I would post a full blown example, but I must admit there are a couple of things off, this uses REDISCLOUD, it is on Heroku, it does work. I'll post this elsewhere and maybe put it in a doc too.
var redis = require('redis');
var ioredis = require('socket.io-redis'); //Adapter
var url = require('url');
var redisURL = url.parse(process.env.REDISCLOUD_URL );
var pub = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
var sub = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
pub.auth(redisURL.auth.split(":")[1]);
sub.auth(redisURL.auth.split(":")[1]);
var redisOptions = {
pubClient: pub,
subClient: sub,
host: redisURL.hostname,
port: redisURL.port
};
io.adapter(ioredis(redisOptions));
Following code works for me with Heroku Redis, hope this helps.
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
var redisAdapter = require('socket.io-redis');
io.adapter(redisAdapter({
pubClient: redis.createClient(process.env.REDIS_URL, {return_buffers: true}),
subClient: redis.createClient(process.env.REDIS_URL, {return_buffers: true})
}));
For the ones interested,
this is my prototype chat server running on newest socket.io with express, multiple cores and redis as an intermediate.
Broadcasted messages are send to all room users no matter if they are connected to a different node and port instance.
Just run
node server.js
and on other machines
node client.js
or for testing node client.js 7001, 7002, 7003 ......
server.js
var options = {
//workers: 2, // total workers (default: cpu cores count).
first_port: 7000, // 8000, 8001 are worker's ports (default: 8000).
proxy_port: 5000, // default (5000).
session_hash: function (req, res) { return req.connection.remoteAddress; },
no_sockets: false // allow socket.io proxy (default: false).
};
require('sticky-socket-cluster')(options, start);
function start(port) {
// requirements
var express = require('express');
var http = require('http');
var socketio = require('socket.io');
var path = require('path');
var sticky = require('sticky-session');
var app = express();
var server = http.createServer(app);
var io = socketio.listen(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
server.listen(port, function() {
console.log(' - listening on ' + port+ ' ' + __dirname);
});
// require our chatserver
var ChatServer = require('./chatserver');
// initialize a new chat server.
new ChatServer({io: io, port: port}).init();
}
chatserver.js
RoomUtil = (function(){
roomMessages = {};
return {
getMessages : function(room_id,limit,cb){
//TODO
cb(roomMessages[room_id] || []);
},
postMessage : function(message,room_id,cb){
if (!roomMessages[room_id]) roomMessages[room_id] = [];
roomMessages[room_id].push(message);
cb();
}
}
})();
var Server = function(options) {
var self = this;
self.io = options.io;
// users array
self.users = [];
// initialize function
self.init = function() {
console.log("init");
// Fired upon a connection
self.io.on('connection', function(socket) {
console.log("incoming connection");
// var ru = new RoomUser();
self.handleConnection(socket,options.port);
});
}
// socket handler for an incoming socket
self.handleConnection = function(socket,port) {
// wait for a login message
socket.emit("incoming connection",{});
socket.on("joinroom",function(data,joinroom_callback){
console.log("attempt to join room ",data.room_id," on port ",port);
if (!data.room_id){
console.log("cannon join room -> no room id given");
return socket.disconnect();
}
else{
var room_id = data.room_id;
socket.join(room_id,function(){
console.log(socket.rooms);
RoomUtil.getMessages(data.room_id,50,function(messages){
console.log("client succesfully joined room ",data.room_id);
joinroom_callback(null,{'messages':messages});
});
socket.on("login",function(data,login_callback){
if (!data.username){
login_callback("invalid userdata",null);
}
else{
login_callback(null,1);
socket.on("post_message",function(data,message_callback){
if (!data.message || data.message == ""){
console.log("empty message posted. ignore");
message_callback("invalid_message",null);
}
else{
console.log("received message on port ",port,data.message);
message_callback(null,1);
RoomUtil.postMessage(data.message,room_id,function(){
RoomUtil.getMessages(room_id,50,function(messages){
console.log("emit messages to room id ",room_id);
//socket.to(room_id).emit('update_messages', messages);
//socket.broadcast.to(room_id).emit('update_messages', messages);
//socket.broadcast.to(room_id).emit('update_messages', messages);
//self.io.to(room_id).emit('update_messages', messages);
self.io.in(room_id).emit('update_messages', messages);
});
})
}
});
}
});
});
}
});
}
}
module.exports = Server;
client.js
var servercon = 'http://localhost:'+(process.argv[2] || 5000);
console.log("going to connect to "+servercon)
var socket = require('socket.io-client')(servercon);
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
socket.on('connect', function(){
console.log("connected, going to login");
socket.emit("joinroom",{"room_id":123123}, function(error,data){
if (error){
console.log("cannot join room ",error);
}
else{
console.log("succesfully joined room -> going to login now");
console.log("received messages count",data.messages.length);
socket.emit("login",{username:"John Mckain"}, function(error, message){
if (error){
console.log("error logging in ",error);
}
else{
console.log("logged in succesfully -> post message now");
var readline = function(){
rl.question("type in a message -> ", function(message) {
socket.emit("post_message",{'message':message}, function(error, message){
if (error){
console.log("error posting message");
readline();
}
else{
console.log("succesfully posted message");
readline();
}
});
});
}
readline();
}
});
socket.on("update_messages",function(data){
console.log("received new messages count ",data.length,data);
});
}
});
});
socket.on('event', function(data){
console.log("event send",data);
});
socket.on('disconnect', function(e){
console.log("disconnected",e);
});
socket.on("welcome",function(data){
console.log("on welcome ",data)
})
socket.on("pong",function(e){
console.log("pong")
})

Message broadcasted multiple times in Socket.io

I am relatively new to node.js and socket.io. While building a sample chat room application I am encountering a case where when I refresh the browser n times, and then send a message, the message gets broadcasted n-times to all the clients. How do I make it not broadcast multiple times and just once? Here goes the server side and client side code.
qaserver.js
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server, { log : false });
server.listen(4040);
var redis = require('redis');
var r43 = redis.createClient('6379', '127.0.0.1');
r43.select("43");
// Set the view directory to /views
app.set("views", __dirname + "/views");
app.use(express.static(__dirname + '/public'));
// Let's use the Jade templating language
app.set("view engine", "jade");
app.get("/", function(request, response) {
response.end("Welcome to the homepage!");
});
app.get("/qaclient", function(request, response) {
response.render("qaclient", { message: "Welcome to QA Forum" });
io.sockets.once('connection', function (socket) {
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(){
// store the username in the socket session for this client
socket.join('test_room');
socket.room='test_room';
console.log("------------- List of connected clients on adduser -------------------");
var clients = io.sockets.clients('test_room'); //
console.log(clients);
//socket.username = username;
socket.emit("welcome_fn",socket.room);
// echo to client they've connected
});
socket.on('message', function (data) {
//io.sockets.in(socket.room).emit('broadcast_message', data.name,data.message);
console.log("------------- List of connected clients on message broadcast -------------------");
var clients = io.sockets.clients('test_room'); //
console.log(clients);
socket.broadcast.to('test_room').emit('broadcast_message', data.name,data.message);
//io.sockets.emit('broadcast_message', data.name,data.message);
console.log(data.name);
console.log(data.message);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
console.log('Socket disconnected : ');
console.log(socket.room);
socket.leave('test_room');
//console.log(socket.leave(socket.room));
});
});
});
--
qaclient.js
window.onload = function() {
var messages = [];
var field = document.getElementById("message");
sendButton = document.getElementById("sendbutton");
//var content = document.getElementById("content");
var name = document.getElementById("name");
var socket = io.connect('http://localhost:4040');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser');
});
socket.on('welcome_fn',function(room)
{
chatcontent.innerHTML = "<b>Welcome to Chat central.. Type your message to start chatting in room "+ room +"</b>";
});
socket.on('broadcast_message', function (name,message) {
var data=new Object();
data.message=message;
data.username=name;
if(message) {
messages.push(data);
var html = '';
console.log(messages.length);
for(var i=0; i<messages.length; i++) {
html += '<b>' + messages[i].username + ': </b>';
html += messages[i].message + '<br />';
}
chatcontent.innerHTML = html;
}
//socket.emit('message', { name: name; message:field });
});
sendButton.onclick = function() {
if($.trim(field.value)=="")
{
alert("Enter the message Foo!");
}
else
{
name_tosend_tmp=name.value;
name_tosend= name_tosend_tmp.replace(/(<([^>]+)>)/ig,"");
message_tosend_tmp=field.value;
message_tosend= message_tosend_tmp.replace(/(<([^>]+)>)/ig,"");
socket.emit('message', { name: name_tosend , message:message_tosend });
var data=new Object();
data.message=message_tosend;
data.username=name_tosend;
messages.push(data);
var html = '';
for(var i=0; i<messages.length; i++) {
html += '<b>' + messages[i].username + ': </b>';
html += messages[i].message + '<br />';
}
chatcontent.innerHTML = html;
document.getElementById("name").value="";
document.getElementById("message").value="";
}
}
}

Resources