i need a udp client wait for udp server to be created to connect and send first message.
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
client.connect(27015, 'localhost', function() {
client.send(message, (err) => {
});
});
this code works when the udp server is already running, but do nothing if I first start the client and then server. I need it to work in both cases.
UDP is a connection-less and an unreliable protocol, there is no way to know if the server is running or not on the client side.
See Difference between TCP and UDP?
Related
I've a GPS tracker device that sends data to my Node.js TCP server and I can send data back to device from the TCP server.
const net = require('net');
const port = 6565;
const host = '127.0.0.1';
const server = net.createServer(onClientConnection);
function onClientConnection(sock){
console.log(`${sock.remoteAddress}:${sock.remotePort} Connected`);
sock.on('data',function(data){
console.log(`${sock.remoteAddress}:${sock.remotePort} Says : ${data} `);
sock.write("Hello World!");
});
//Handle client connection termination.
sock.on('close',function(){
console.log(`${sock.remoteAddress}:${sock.remotePort} Terminated the connection`);
});
//Handle Client connection error.
sock.on('error',function(error){
console.error(`${sock.remoteAddress}:${sock.remotePort} Connection Error ${error}`);
});
};
server.listen(port,host,function(){
console.log(`Server started on port ${port} at ${host}`);
});
However I'm looking to extend this device/server interaction to a web portal from where I want to send/receive data from the device in real-time and I can't seem to wrap my head around how to approach this problem. Is it possible to do this? The device itself is a low-end device that don't seem to have a embedded web server. Can we create a REST API like interface between the web portal and TCP server to accomplish this task? I'm really lost. Any pointers?
Please help.
What are the steps to complete a poll call directly from Javascript (NodeJS) to Kafka (not the HTTP endpoints or proxies), without a library? Examples would be wonderful, but even steps and directions are great.
So for instance, authentication, subscription, etc?
PS: This is mostly to learn the internals of communicating with Kafka.
to Kafka (not the HTTP endpoints or proxies)
Kafka uses a custom TCP protocol, and not HTTP, so what you're looking for doesn't exist.
It'd be suggested to use a library rather than attempt to rewrite this TCP socket communication yourself, but if you wanted to, a completely blank slate would start with
const net = require('net');
const client = new net.Socket();
// Send a connection request to the server.
client.connect({ port: 9092, host: 'kafka' }), function() {
console.log('TCP connection established with the server.');
client.write(...); // some binary data matching Kafka spec
});
client.on('data', function(chunk) {
console.log(`Data received from the server: ${chunk}.`);
});
client.on('end', function() {
console.log('Requested an end to the TCP connection');
});
I have been really struggling to send data from Matlab over a network to a series of 'Dashboards' written in HTML/JS that essentially just display the data.
In Matlab I use uSend = udpport("datagram","IPV4","LocalHost","127.0.0.1","LocalPort",3333) then write(uSend,D,"char","LocalHost",2560) to send an array D=jsonencode([1,2,3,4,5]) to port 2560.
My current implementation uses NodeJS 'dgram' to receive the data. The implementation below works:
const dgram = require('dgram');
const socket = dgram.CreateSocket('udp4');
socket.on('message', (msg,rinfo) => {
console.log(`Data Received: ${JSON.parse(msg)}`)
})
socket.bind(2560,"127.0.0.1")
BUT: This only works with NodeJS i.e. run the script above node script.js. The 'Dashboards' need to run essentially on a chrome browser, which dgram won't work (not even with browserify, it's not a supported function).
Hence, my hands are sort of tied, with Matlab I can realistically only send UDP (it's multicast) and I can't get UDP data on the JS side of things.
I was wondering, with webRTC, is it possible to get it to listen to a port? e.g. something like webRTC listen to port 2560 #127.0.0.1?
Any help would be much appreciated! I am relatively new to programming so I may be asking the wrong question here. Thanks
WebRTC requires a lot more then just a UDP port unfortunately.
I would suggest instead running a node.js server that accepts incoming multicast traffic and caches like this.
const dgram = require('dgram');
const socket = dgram.CreateSocket('udp4');
let data = [];
socket.on('message', (msg,rinfo) => {
data.push(JSON.parse(msg))
})
socket.bind(2560,"127.0.0.1")
Then I would provide a HTTP endpoint that returns the JSON. You can have the browser poll this at an interval of your choosing.
const http = require('http');
const server = http.createServer((req, res) => {
response.end(JSON.stringify(data));
})
const port = 3000
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
})
You can then test this by doing curl http://localhost:3000. You are just caching the data then making it available via HTTP request.
In the future I would look into using a Websocket instead. This way the server can push the data to the browser as soon as it is available. Using a HTTP endpoint requires the browser to request on an interval.
I'm trying communicate between a Node.js socket (I am using zmq) and an external tcp socket via ZMQ.
The external socket (tcp://***.***.***.***:5555) is a part of a C++ service, and he acts like a dealer.
The Node.js server should act like a router, which should monitor and pass messages to the available workers (that should be received from the external tcp socket).
The connection is successfully made between both services, but once I am connected to the tcp socket, I don't receive any message from the external service.
# Node.js server
let zmq = require('zmq');
socket = zmq.socket('router');
// Successfully connected
socket.on('connect', () => {console.log('Connected!')});
// No message received from tcp
socket.on('message', (message) => {console.log('Message: ', message)});
socket.monitor(500, 0);
socket.connect('tcp://***.***.***.***:5555');
Any thought will be very welcomed!
I have a device in my local that sends JSON data through TCP:
ex. {"cmd": "listactions", "id": 13, "value": 100}
The data is send from 192.168.0.233 on port 8000
(I've checked this with wireshark)
I would like to (with my homeserver) intercept those specific commands with nodejs & send them through with pusher.
I have very little experience with nodejs & can't seem to get this working. I tried the following:
net = require('net');
var server = net.createServer(function(sock) {
sock.setEncoding('utf8');
sock.on('data', function (data) {
// post data to a server so it can be saved and stuff
console.log(data);
// close connection
sock.end();
});
sock.on('error', function (error) {
console.log('******* ERROR ' + error + ' *******');
// close connection
sock.end();
});
});
server.on('error', function (e) {
console.log(e);
});
server.listen(8000, '192.168.0.233', function(){
//success, listening
console.log('Server listening');
});
But it complaints about EADDRNOTAVAIL, from research I've learned that this means the port is in use... Which I don't understand because I'm able to with Wireshark.
What's the correct way to do this?
Wireshark doesn't listen on all ports. It uses a kernel driver (with libpcap or winpcap) to "sniff" data off the wire. That is, it doesn't sit in between anything. It's not a man-in-the-middle. It just receives a copy of data as it is sent along.
Your application needs to listen on that port if you expect it to receive data. You should use netstat to figure out what process is listening on port 8000 and kill it. Then, you can listen on port 8000.
You could in theory use libpcap to get your data but that's crazy. There is a ton of overhead, something has to listen on that port anyway (it's a TCP connection after all), and it's extremely complex to filter out what you want. Basically, you would be reimplementing the network stack. Don't.