Talking AWS Servers (EC2) - node.js

I am trying to get two EC2 instances to talk; but, I am running into a block. I am not sure what IP address and port to use, and also am wondering if I need to change any of the server/client code now that they aren't talking on the same machine. Security Groups are allowing inbound traffic on ports 22,80, and 60201.
When I use port 60201, i get EADDRNOTAVAIL.
Here is the config file.
Host wmnode
User ubuntu
IdentityFile ~/.ssh/wmnode
HostName 52.4.212.112
Host mvclient
User ubuntu
IdentityFile ~/.ssh/mvclient
HostName 107.21.65.254
Here is server.js
const port = 60201;
const ipAddress = "52.4.212.112"
var http = require('http');
var io = require('socket.io');
var server = http.createServer();
server.listen(port, ipAddress);
var socket = io.listen(server);
socket.on('connection', (serverSocket) => {
console.log('connected to 52.4.212.112');
serverSocket.on('clientEvent', function (data) {
console.log('message from the server:', data);
serverSocket.emit('serverEvent', "thanks for sending '" + data + "'");
});
});
And here is client.js
const port = 60201;
const ipAddress = "52.4.212.112";
const url = 'http://' + ipAddress + ':' + port;
var io = require('socket.io-client');
var socket = io(url);
socket.on('connect', () => {
socket.on('serverEvent', function (data) {
console.log('new message from the server:', data);
});
setInterval(function () {
socket.emit('clientEvent', Math.random());
console.log('message sent from the client');
}, 3000);
});

Related

Forward Android TCP Connection to HTTP proxy in Node.js

My plan is to create a proxy of my phone 4g connection.
I've made a direct tcp connection of my phone to my pc via nodejs.
I create a client.js on my phone and server.js on my pc. They connect.
Now i need to 'transform' this direct connection into a proxy, but i dont know how.
Any help i would aprecciate.
I will show my server.js and client.js code below.
Server.js
var net = require('net');
var tcpServerPort = 7000;
// a simple TCP server for testing
var server = net.createServer(function (socket) {
console.log('Client connected to server');
socket.on('close', function () {
console.log('Client disconnected from server');
});
socket.on('data', function (buffer) {
// 'echo' server
socket.write(buffer);
});
socket.on('error', function (err) {
console.log('Error: ' + err.soString());
});
});
server.listen(tcpServerPort);
Client.js
const net = require('net');
const client = new net.Socket();
const port = 7000;
const host = 'my home ip';
client.connect(port, host, function() {
console.log('Connected');
client.write("Hello From Client " + client.address().address);
});
client.on('data', function(data) {
console.log('Server Says : ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});

Socket.io server to server (sending message to specific server)

I have a specific situation. I have client (browser) which connects to server A. Server A is connected to server B and C. When client sends message to server A, I need to send message to specific server which means to one of the servers B or C, not both of them. Generally I need separated communication between servers but I have only broadcasted communication. Message from server A to another server can't be seen on all connected servers.
How can I send messages directly to one of the servers ? Here is my code snippet for servers. I just change ports for each server and connect them.
// Load requirements
var PORT = process.env.PORT || 8080;//just change port for other servers
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
app.use(express.static(__dirname + '/public'));
var client = require('socket.io-client');
var socket1 = client.connect('http://localhost:8081', { reconnect: true });//connection to server B
var socket2 = client.connect('http://localhost:8082', { reconnect: true });//connection to server C
// Add a connect listener
io.on('connection', function (socket) {
console.log('Client connected.');
//when server receive message from client
socket.on('message_from_browser', function (message) {
console.log("Message from browser broadcasted: " + message.text);
var updated_message = {
text: message.text,
port: PORT
};
// send message to server B
socket1.emit('server_message', updated_message);//send message to server B
});
// Disconnect listener
socket.on('disconnect', function () {
console.log('Client disconnected.');
});
});
socket1.on('connect', function () {
socket1.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
//change server name
console.log('Connected to server B!');
});
socket2.on('connect', function () {
socket2.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
//change server name
console.log('Connected to server C!');
});
http.listen(PORT, function (req, res) {
console.log("Server Started on port: " + PORT);
});
You have a naming conflict with the variable name socket. It is defined as both your connection to server B and as an argument in your .on('connection', function(socket) {...}) callback.
To fix that, change this:
var socket = client.connect('http://localhost:8081', { reconnect: true });//connection to server B
var socket2 = client.connect('http://localhost:8082', { reconnect: true });//connection to server C
To this:
var socket1 = client.connect('http://localhost:8081', { reconnect: true });//connection to server B
var socket2 = client.connect('http://localhost:8082', { reconnect: true });//connection to server C
And, then refer to the server B connection as socket1, not socket.
The variable named socket is already defined as an argument in this so you cannot reach the outer socket variable when it has a conflicting name:
// Add a connect listener
io.on('connection', function (socket) {
// socket variable name ^^^^^^
console.log('Client connected.');
//when server receive message from client
socket.on('message_from_browser', function (message) {
console.log("Message from browser broadcasted: " + message.text);
var updated_message = {
text: message.text,
port: PORT
};
// send message to server B
socket1.emit('server_message', updated_message);//send message to server B
});
// Disconnect listener
socket.on('disconnect', function () {
console.log('Client disconnected.');
});
});
And there were no listeners for incoming server messages so you need to add listeners for socket1 and socket2.
io.on('connect', function(socket1){
socket1.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
});
io.on('connect', function(socket2){
socket2.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
});
});
Use one variable and configs to track others hosts, try:
Updated to fix some bugs:
// Load requirements
var PORT = process.env.PORT || 8081;//just change port for other servers
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var async = require('async'); // async is optional for this example ...
var client = require('socket.io-client');
app.use(express.static(__dirname + '/public'));
// save hosts here ...
var otherServers = {
server1: {
url: 'http://localhost:8082'
},
server2: {
url: 'http://localhost:8083'
},
};
// Add a connect listener
io.on('connection', function (socket) {
console.log('Client connected.');
//when server receive message from client
socket.on('message_from_browser', function (message) {
console.log("Message from browser broadcasted: " + message.text);
var updated_message = {
text: message.text,
port: PORT
};
// choice how server will receive this message ...
// then send it
otherServers.server1.client.broadcast.emit('server_message', updated_message);
});
// Disconnect listener
socket.on('disconnect', function () {
console.log('Client disconnected.');
});
});
async.each(otherServers, function forEachOtherServer(otherServer, next) {
//connect to another server
otherServer.client = client.connect(otherServer.url, { reconnect: true });
// to something more if need in every client connection ...
otherServer.client.on('connect', function (x) {
otherServer.client.on('server_message', function (message) {
console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
//socket.broadcast.emit('message_from_server', message_server);
});
console.log('Connected!');
});
next();
}, function afterConnectInAllServers(err) {
if (err) throw err;
// to something after connect in all servers ...
});
http.listen(PORT, function (req, res) {
console.log("Server Started on port: " + PORT);
});

how to test a tcp server of nodejs

I created a simple tcp server using code
var net = require('net');
// Setup a tcp server
var server = net.createServer(function (socket) {
socket.addListener("connect", function () {
console.log('hello');
sys.puts("Connection from " + socket.remoteAddress);
socket.end("Hello World\n");
});
});
server.listen(7000, "127.0.0.1");
console.log("TCP server listening on port 7000 at 127.0.0.1");
It started successfully, but how can I send some test msg to that tcp server, I tried SocketTest v.3 but not console output.
Use data event handler to receive data in the server.
socket.on('data', function(data) {
console.log(data);
// echo back
socket.write('You said '+data);
});
For people still looking for answers, here's how I did it with SocketTest
const net = require('net');
const client = new net.Socket();
const HOST = "127.0.0.1";
const PORT = 7000;
client.on('data', () => {
client.destroy(); // kill after response
});
client.on('error', () => {
client.destroy(); // kill
});
client.connect(PORT, HOST, () => {
client.write('Hello world!');
client.end();
});

node.js listen for UDP and forward to connected http web clients

I'm new to node.js, so forgive the ignorance if this is simple.
What I want to do is setup a simple node.js http server to which a web-client connects. I also want the node.js server to act as a UDP listener on a separate port, on which it will receive JSON payloads from some other application. I want the node.js server to then forward these JSON payloads immediately to one or more of the connected web-clients.
I got this far from some initial googling around:
Create a simple node.js http server that responds with a static html page:
//Initialize the HTTP server on port 8080, serve the index.html page
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-type': 'text/html'});
res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
console.log('Listening at: 127.0.0.1 8080');
}
);
Initialize a UDP server on a separate port:
//Initialize a UDP server to listen for json payloads on port 3333
var srv = dgram.createSocket("udp4");
srv.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
io.sockets.broadcast.emit('message', 'test');
//stream.write(msg);
//socket.broadcast.emit('message',msg);
});
srv.on("listening", function () {
var address = srv.address();
console.log("server listening " + address.address + ":" + address.port);
});
srv.bind(5555);
Use socket.io to establish a live connection between web-client and server:
//this listens for socket messages from the client and broadcasts to all other clients
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.on('message', function (msg) {
console.log('Message Received: ', msg.data.skeletons[0] ? msg.data.skeletons[0].skeleton_id : '');
socket.broadcast.emit('message', msg);
}
);
});
I guess my problem is I don't know how to bridge 2 and 3, to get the received UDP packets broadcasted to the connected socket.io clients. Or perhaps there's a simpler, more elegant way of doing this? I found the documentation for socket.io to be lacking...
EDIT: thanks to the person that fixed the code formatting
I made a running example for you to get going with: http://runnable.com/UXsar5hEezgaAACJ
For now it's just a loopback client -> socket.io -> udp client -> udp server -> socket.io - > client.
here's the core of it:
var http = require('http');
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/index.html');
//Initialize the HTTP server on port 8080, serve the index.html page
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-type': 'text/html'
});
res.end(html);
}).listen( process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP, function() {
console.log('Listening');
});
var io = require('socket.io').listen(server);
io.set('log level', 0);
io.sockets.on('connection', function (socket) {
socket.emit('message', 'connected');
socket.on('message', function (data) {
console.log(data);
var address = srv.address();
var client = dgram.createSocket("udp4");
var message = new Buffer(data);
client.send(message, 0, message.length, address.port, address.address, function(err, bytes) {
client.close();
});
});
});
var dgram = require('dgram');
//Initialize a UDP server to listen for json payloads on port 3333
var srv = dgram.createSocket("udp4");
srv.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
io.sockets.emit('message', 'udp');
});
srv.on("listening", function () {
var address = srv.address();
console.log("server listening " + address.address + ":" + address.port);
});
srv.on('error', function (err) {
console.error(err);
process.exit(0);
});
srv.bind();

Create WebSockets between a TCP server and HTTP server in node.js

I have created a TCP server using Node.js which listens to clients connections.
I need to transmit data from TCP server to HTTP server again in Node.js possibly through a Websocket (socket.io).
However, I do not know how to create such connection such that TCP server is able to push data to HTTP server through Websocket.
Many Thanks.
I was trying lot of things to get this work. Most of the time I was relying on socket.io to get this working, but it was just not working with TCP.
However, net.Socket suffices the purpose.
Here is the working example of it.
TCP Server
var net = require('net');
var HOST = 'localhost';
var PORT = 4040;
var server = net.createServer();
server.listen(PORT, HOST);
server.on('connection', function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
sock.write("TCP sending message : 1");
console.log('Server listening on ' + server.address().address +':'+
server.address().port);
}).listen(PORT, HOST);
HTTP Server
var http = require('http').createServer(httpHandler),
fs = require("fs"),
wsock = require('socket.io').listen(http),
tcpsock = require('net');
var http_port = 8888;
var tcp_HOST = 'localhost';
var tcp_PORT = 4040;
/**
* http server
*/
function httpHandler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
http.listen(http_port);
console.info("HTTP server listening on " + http_port);
wsock.sockets.on('connection', function (socket) {
var tcpClient = new tcpsock.Socket();
tcpClient.setEncoding("ascii");
tcpClient.setKeepAlive(true);
tcpClient.connect(tcp_PORT, tcp_HOST, function() {
console.info('CONNECTED TO : ' + tcp_HOST + ':' + tcp_PORT);
tcpClient.on('data', function(data) {
console.log('DATA: ' + data);
socket.emit("httpServer", data);
});
tcpClient.on('end', function(data) {
console.log('END DATA : ' + data);
});
});
socket.on('tcp-manager', function(message) {
console.log('"tcp" : ' + message);
return;
});
socket.emit("httpServer", "Initial Data");
});
Browser Client
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('httpServer', function (data) {
console.log(data);
document.write(data + "\r\n");
socket.emit('tcp', "For TCP");
});
</script>
This way, there is a socket opened between HTTP server and TCP server in Node.js.
If you need to communicate server-server than websockets is probably not a best choice. Try one of RPC libraries, or just use HTTP or your own protocol.
You can use either socket.io or ws (only WebSocket) on Node.js as client (not only in browser)
var io = require('socket.io-client');
var socket = io.connect('http://IP address of Websocket server');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});

Resources