Unable to receive IPv6 UDP multicast message with Node.js - node.js

I have setup two PC in the local network. One is Linux PC where i am running a Script, sending Heart beat message every 1s over UDP as IPv6 multicast message (FF02::1). In the other windows PC, I am running the below node.js code to receive the Heart beat message. I can see the messages in the Wireshark log, but i cant read the message from my code.
var HOST = '::';
var port = 50010;
var dgram = require('dgram');
var message = new Buffer('(data)');
var client = dgram.createSocket('udp6');
// client.bind(port,HOST);
// client.setMulticastLoopback(true);nod3
client.on("error",function(err){
console.log("server error:\n"+err.stack);
})
client.on("message",function(msg,rinfo){
console.log("Received Message:",msg+"from"+rinfo.address+":"+rinfo.port);
console.log("Node address:"+ rinfo.address);
console.log("Node port:"+rinfo.port);
})
client.bind('50010', '::', () => {
client.addMembership('ff02::1', '::%17');
});
client.on("listening",function(){
var address = client.address();
console.log("server listioning"+":"+address.address+":"+address.port)
});

Related

How to recive data from a Flightcell DZMX via Iridium SBD

I have a Flightcell DZMX configured to send data to a ip and port via iridium SBD, where i have a server with a simple code running that recive any request and display on the screen:
var net = require('net');
var server = net.createServer(function(socket) {
socket.on('data', function(data) {
console.log(data.toString());
})
});
server.listen(8080, 'my-server-ip-adress');
console.log('Server listening on port 8080');
But no message is recived from the DZMX. Is iridium SBD not in TCP protocol or is my DZMX misconfigured?
It is inded TCP/IP.
You need to configure your equipament IMEI, IP and port in: https://spnetpro.iridium.com
Everything working fine now.

Node JS: UDP Client does not work in OSX (11.2.2)

I have a windows machine running a udp-server and I try to create a UDP->WebSocket Bridge using node-js...
But I just came as far as connection to the server which seems to run nice on windows but fails on osx.
Using this code:
var PORT = 8005;
var HOST = '192.168.1.33';
var dgram = require('dgram');
var message = new Buffer.from('CONNECTED');
var client = dgram.createSocket('udp4');
client.on('listening', function () {
var address = client.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
client.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
});
client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + HOST +':'+ PORT);
});
Windows outputs nice:
UDP Server listening on 0.0.0.0:65054
UDP message sent to 192.168.36.1:8005
192.168.36.33:8005 - OK
OSX Just:
UDP Server listening on 0.0.0.0:50360
UDP message sent to 192.168.36.1:8005
The Windows server reports a socket error which normaly means there is a network-issue.
I wonder where the problem is, looks like OSX blocks the udp-connection for some reason but I have no firewall or other filter activated or installed on my mac.
Any idea?
Seems the problem is solved, when I disable Wifi and just use my ethernet connection. Seems to be an issue when two network interface are available. Maybe it sends out on device1 but listens on device2...

Passing OSC messages between two computers over WAN via UDP and Node.js

I'm trying to write some minimalist client-server code to pass OSC messages between two computers that are on different local networks. Specifically my end goal is to send and receive from MAX patches in Ableton, using MAX's UDPSend and UDPReceive functions, which simply listen on local ports. Here is the server code, that I have Ableton's OSC Send MIDI module send to. This correctly receives output from the first computer's MAX patch, some MIDI data from a channel.
var osc = require('osc-min'),
dgram = require('dgram');
var remotes = [];
var FileReader = require('fs');
// listen for OSC messages and print them to the console
var udp = dgram.createSocket('udp4', function(msg, rinfo) {
try {
var oscmsg = osc.fromBuffer(msg);
console.log(oscmsg);
remotes.forEach(function(remote, index, array) {
udp.send(msg, 0, oscmsg.length, 10000, remote);
console.log('Sent OSC message to %s:%d', remote, 10000);
});
} catch (err) {
console.log(err);
}
});
FileReader.readFile('ips.txt', 'utf8', function(err, data) {
// By lines
var lines = data.split('\n');
for(var line = 0; line < lines.length; line++){
remotes.push(lines[line]);
}
});
udp.bind(9998);
console.log('Listening for OSC messages on port 9998');
The server correctly gets an OSC message like this:
{ address: '/Note1',
args: [ { type: 'integer', value: 60 } ],
oscType: 'message' }
I then want to relay Computer 1's message to my remote server on port 9998 to the rest of the computers I have listening on port 10000. (The server is an Amazon EC2 Ubuntu instance.) The public IPs of these computers are in a file ips.txt.
On the receiving computers (testing on Windows), I set up a Node client to receive data via its public IP and relay it to localhost so the MAX patch can see it. Thus, any messages sent to 10000 on this computer will be sent to 127.0.0.1:9999.
var osc = require('osc-min'),
dgram = require('dgram'),
remote;
// listen for OSC messages and print them to the console
var udp = dgram.createSocket('udp4', function(msg, rinfo) {
try {
var oscmsg = osc.fromBuffer(msg);
console.log(oscmsg);
send(msg);
} catch (err) {
console.log('Could not decode OSC message');
}
});
function send(oscMessage) {
udp.send(oscMessage, 0, oscMessage.length, 9999, "127.0.0.1");
console.log('Sent OSC message to %s:%d', "127.0.0.1", 9999);
}
udp.bind(10000);
console.log('Listening for OSC messages on port 10000');
The problem is, the server's message never seems to reach the client. Once the server receives the message, it produces output as such:
Listening for OSC messages on port 9998
{ address: '/Velocity1',
args: [ { type: 'integer', value: 100 } ], oscType: 'message' }
Sent OSC message to client-public-ip:10000
But the client simply hangs on it's initial listening state
Listening for OSC messages on port 10000
I thought this may be a problem with my firewall, and I made sure to enable UDP specifically through the port and on all the applications involved, to no gain. I'm hoping to avoid using HTTPRequests. I'd greatly appreciate any suggestions.
Edit: I created a diagram to help visualize what's supposed to be going on between these three computers. The dashed boxes represent the local network that computer is on (they are all on different networks). I hope this helps a bit.
Daniel's solution to use WebSockets is correct. However, I also needed to be sure to connect the clients via the DNS name rather than the public ip address. For example:
const WebSocketClient = require('websocket').client;
const port = process.env.PORT || 3000;
const wsc = new WebSocketClient();
wsc.connect('ws://ec2.compute-1.amazonaws.com:4000/', 'echo-protocol');
As opposed to:
wsc.connect('ws://1.2.3.4:4000/', 'echo-protocol');
More details about this problem here.
I also used the 'websocket' npm module instead of 'ws,' but I'm not sure that made a difference.
The easiest way I've found to get this working between different LANs
and WAN was to proxy the UDP requests and send them to an Echo server using Websockets. Here is an example:
Echo Server (WAN)
"use strict";
const WebSocket = require('ws');
const port = process.env.PORT || 3000;
const wss = new WebSocket.Server({ port: port });
// Broadcast to all
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
wss.on('connection', function connection(ws, req) {
const ip = req.connection.remoteAddress;
console.log('connecting to', ip);
ws.on('message', function incoming(data) {
// Broadcast to everyone else.
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
});
});
UDP Server / WebSocket Client (Local)
const Buffer = require('buffer').Buffer;
const dgram = require('dgram');
const WebSocketServer = require('ws').Server;
const port = process.env.PORT || 3000;
const wss = new WebSocketServer({port});
//The ip port of the UDP server
var SERVER_IP = '127.0.0.1'
var SERVER_PORT = 11000
wss.on('connection', function(ws) {
//Create a udp socket for this websocket connection
let udpClient = dgram.createSocket('udp4');
//When a message is received from udp server send it to the ws client
udpClient.on('message', function(msg, rinfo) {
ws.send(msg.toString());
});
//When a message is received from ws client send it to UDP server.
ws.on('message', function(message) {
var msgBuff = new Buffer(message);
udpClient.send(msgBuff, 0, msgBuff.length, SERVER_PORT, SERVER_IP);
});
});
Testing
You can configure Ableton connecting to the local UDP server, in this example de default address would be something like udp://localhost:11000.

Net Socket Connections in OpenShift

I am trying to create a net socket Nodejs server for my embedded device to send data to on OpenShift.
I am trying to create this simple echo service
var net = require('net');
var HOST = process.env.OPENSHIFT_NODEJS_IP;
var PORT = process.env.OPENSHIFT_NODEJS_PORT || 3000;
console.log('IP:' + HOST + ' Port:' + PORT);
var server = net.createServer(function(connection) {
console.log('client connected');
connection.on('end', function() {console.log('client disconnected');});
connection.write('Hello World!\r\n');
connection.pipe(connection);
});
server.listen(PORT, HOST , function() {
console.log('server is listening');
});
I according to OpenShift's Port Binding Guide I had my client application connect to Port 8000.
For Testing I am using the following code from my desktop machine.
var net = require('net');
var HOST = 'nodejs-myapplication.rhcloud.com';
var PORT = 8000;
var client = net.connect(PORT, HOST, function() {
console.log('connected to server!');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('disconnected from server');
});
The Client Scripts gets to Connected to server and gets stuck there itself. Nothing takes place after that.
Now if I open the address nodejs-myapplication.rhcloud.com:8000 in my browser, the NodeJS Server logs a client connected and disconnected, but when the NodeClient is connected the server doesn't show any update. The Node Client just says connected and stays there without doing anything.
If I run the same scripts locally it works fine, ( Locally i use HOst as 127.0.0.1 and port as 3000).
How can I get a TCP Application to connect to the Openshift NodeJS Server?
The Device will be sending ASCII output over the socket eg. $VAR,12,23,21\r\n
I need to be able to read that data, convert it to JSON and send it out to another server.
It has to be loaded on a platform like DigitalOcean with a firewall enabled.
OpenShift doesn't allow custom ports by default so need a workaround for that.

Node.js Remote UDP server does not receive messages

I have a basic problem with a UDP server in Node.js, I've used this little example:
// Remote server
var PORT = 3030;
var HOST = '127.0.0.1'
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
server.on('message', function (message, remote) {
console.log('Message received')
})
server.bind(PORT, HOST);
When I try to send a message from my computer, the server does not respond:
$ echo "test" | nc -u <server_ip> 3030
but, when I try to send the message from the server itself, the message arrives.
$ Message received
The server has static ip and I don't think it's a problem with the ports. Any idea? Thanks.
In the past I've work with the unix type for this kind of socket. It may work with unix_dgram as follow
var server = dgram.createSocket('unix_dgram');
Your server is only listening on localhost. You need to change your HOST variable to something else to be able to listen for messages from the outside world (e.g. 0.0.0.0 for all addresses, or a specific IP like 192.168.1.101).
EDIT: I'm assuming that there is no router in between.

Resources