TCP Communicator Application? - debugging

I am looking for software to help debug client/server software.
I am looking for an application where I can simply connect to a remote host on a given port and send it bytes of data and observe the responses.
Is there such software existing?

Use netcat. There are many different implementations. This is the manual for BSD's netcat.

nodejs do have an high level tcp api you can use to create a 5lines script that wee do the job for you
var net= require('net');
var sock = net.connect(1234, '127.0.0.1', function(){
sock.write(new Buffer("some binary data here"));
sock.write(new Buffer([0,1,2])); // 3 more bytes
sock.on("data", function(buffer) {
console.log("Server response is ", buffer); // display raw response buffer
})
});

Related

packet loss socket io

I encountered such a problem. When online, about 1000 people start having problems with socket io. Some messages are not received to the recipients. The loads are approximately the following: a message is sent 2 times per second to all clients. At the moment there is a cluster of 6 servers, all clients are evenly distributed across the socket io servers, but there are still losses. On the server side, I use io + redis socket. PHP sends a message to redis, and then the server on node js reads it and sends it to all clients on sockets. Server Code:
redis.psubscribe('*');
redis.on('pmessage', (subscribed, channel, message) => {
message = JSON.parse(message);
io.to(channel).emit(channel, message.data);
});
/** SOCKET.IO **/
io.on('connection', socket => {
socket.on('subscribe', data => {
socket.join(data.channel)
});
});
What would you advise? Can use redis + socket io not the best option? All traffic goes through cloudflare. Can there be losses because of this?

How many WebSocket connection can be created on Client Side

I have started to learn web sockets. It is must learn technology in today's time.
But i am curious to learn more about it. My basic question is How many WebSocket connection can be created on Client Side.
My Typically Application is html UI based and on the server i have rest based services. I need to track whether
Session timeout has happed or not
Whether Connection to the server is lost or not ? A kind of pooling program to check with connections is alive or not.
So I am creating 2 websocket objects on client and different url for them.
I hope i have implemented it correctly ?
Basically Browser closes the old websocket connection once you opened to new connection to SAME URL(ws://127.0.0.1:8080/WebSocket-context-root/getResource). You can keep small hack like "ws://127.0.0.1:8080/WebSocket-context-root/getResource/"+k. where k is any number/any random string. On server side just ignore the path variable k.
In this way you can open many number of connection at same time. Browser restriction of max-number-connection per domain is not applying here (Tested on Firefox). I tried max 25 parallel connections.
You can use websocket.readyState to check the status of the web socket connection.
onclose Event of the Web socket have reason code for closed connection.
User below code to test number of active connections.
var x=0
var intervalID = setInterval(function () {
websocket = new WebSocket("ws://127.0.0.1:8080/WebSocketApi/web/chat/"+x);
websocket.onopen = function (evt) {
console.log('open')
}
websocket.onmessage = function (evt) {
console.log('msg');
}
websocket.onclose= function (evt) {
console.log('closed');
}
if (++x === 15) {
window.clearInterval(intervalID);
}
}, 1);

Exchange data between node.js script and client's Javascript

I have the following situation, where the already sent headers problem happens, when sending multiple request from the server to the client via AJAX:
It is something I expected since I opted to go with AJAX, instead of sockets. Is there is other way around to exchange the data between the server and the client, like using browserify to translate an emitter script for the client? I suppose that I can't escape the sockets, so I will take advice about simpler library, as sockets.io seems too complex for such a small operation.
//-------------------------
Update:
Here is the node.js code as requested.
var maxRunning = 1;
var test_de_rf = ['rennen','ausgehen'];
function callHandler(word, cb) {
console.log("word is - " + word);
gender.gender_function_rf( word , function (result_rf) {
console.log(result_rf);
res.send(result_rf);// Here I send data back to the ajax call
setTimeout(function() { cb(null);
}, 3000);
});
}
async.eachLimit(test_de_rf, maxRunning, function(item, done) {
callHandler(item, function(err) {
if (err) throw new Error(err);
done();
});
}, function(err) {
if (err) throw new Error(err);
console.log('done');
});
res.send() sends and finishes an http response. You can only call it once per request because the request is finished and done after calling that. It is a fairly high level way of sending a response (does it all at once in one call).
If you wanted to have several different functions contributing to a response, you could use the lower level functions on the http object such as res.setHeader(), res.writeHead(), res.write() (which you can call multiple times) and res.end() (which indicates the end of the response).
You can use the standard webSocket API in the browser and get webSocket module for server-side support or you can use socket.io which offers both client and server support and a number of higher level functions (such as automatic reconnect, automatic failover to http polling if webSockets are not supported, etc...).
All that said, if what you really want is the ability to just send some data from server to client whenever you want, then a webSocket is really the better way to go. This is a persistent connection, is supported by all modern browsers and allows the server to send data unsolicited to the client at any time. I'd hardly say socket.io is complex. The doc isn't particularly great at explaining things (not uncommon in the open source world as the node.js doc isn't particularly great either). But, I've always been able to figure advanced things out by just looking at a few runtime data structures in the debugger and/or looking at the source code.

socket.io not working inside zmq function

i am putting socketio inside zmq but its not working below is the code
not sure what i am doing wrong
var io = require('socket.io').listen(2939);
var zmq = require('zmq');
var socket = zmq.socket('sub');
console.log("Connecting to Currency Stream....");
socket.subscribe("");
socket.on('message', function (data){
//console.log(data.toString());
io.sockets.on('connection', function(socket){
console.log("In socket.io");
console.log(data.toString());
socket.emit("live_rates",
{date: data[0], time: data[1],
open: data[2], high: data[3],
low: data[4], close: data[5]});
socket.emit("news", {hello: 'world'});
console.log("emitted");
});
});
socket.connect('tcp://localhost:3191');
code inside io.socket.on is not running, handshake is successful as i can see in the log window. when i am putting the code inside zmq (uncomment //console.log(data.toString()); )
it's showing the data.
what i am really doing here is getting the data from zmq server on a zmq tcp port 3191 and forward it to socket.io http port 2939, then getting the data on browser by connecting to port 2939 using an html page. i do not wish to use zmq on client side as socket.io is much robust for that play.
also a separate socket.io connection ie io.sockets.on is working but inside a zmq socket it fails!
any one
zishan
solved it by using io.sockets.emit rather than socket.emit.
io.socket.on is now placed outside of zmq.
i suspect socketio callback is now responding if using socket.emit when using inside zmq function.
got a clue from this blog post
https://groups.google.com/forum/#!msg/socket_io/Vi0uWeHsj_0/JEOcXAc-pn8J

socket.io - How to broadcast a message to all open sockets

I would like to broadcast a message to all open sockets as a result of a non-socket related event, e.g. as a result of a timeout. How do I do this? Should I keep an array of all open sockets and send them a message one by one? Is there a better way?
Note: The broadcasting example in socket.io guide sends a broadcast message in response to a socket connection, so it has a handle to a socket. Even then it says
Broadcasting means sending a message to everyone else except for the socket that starts it.
Edit
To clarify my question - I want to "send a message" to all open sockets. This action is NOT triggered by any socket, so there is no "this socket". For example, some business logic on the server decides that an order is now executed. This information needs to be sent to all open sockets. What's the best way to do this? (This is not "broadcasting" as socket.io defines it.)
So basically you need to get all connected clients to your socket
var clients = io.sockets.clients(); // This returns an array with all connected clients
for ( i = 0; i < clients.length; i++ ) {
clients[i].emit('event', { data: 'message' });
}
This will emit to all of your connected clients.
socket.broadcast.emit('event name', { data : 'your data' });
It will broadcast to all open sockets, except this socket.

Resources