Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

App for UDP communication which listens for datagrams.

Notifications You must be signed in to change notification settings

BartekCK/node-udp-communication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UDP communication with Node.js

TCP is everyhere, so everyone should rest sometimes 😎
I made simple server which listen to datagrams and do actions with file system by 'fs' module.

This example is a small antipattern. We shouldn't send a datagram with commands for file system manage (but it depends ...). If we send command for delete file, but file not exist, client should get a response with error, but this is not TCP ☺️
So in this kind of problem client/user cannot get information about transaction.

I created also UDP client with Node for show faster way to see result. For both app I use 'dgram' and 'fs' module in server part.

File system server commands

Server for fs management should get a special data buffer. In this kind I use a JSON.

  • Create file
{
    "action": "CREATE_FILE",
    "fileName": "test.txt",
}
  • Update file
{
    "action": "WRITE_INTO_FILE",
    "fileName": "test.txt",
    "content": "ala ma kota"
}
  • Delete file
{
    "action": "DELETE_FILE",
    "fileName": "test.txt",
}

Start server

Go to node-udp-communication/udp-server and exc commands:

npm i
npm start

Execute commands for client (start server before this step)

With netcat

client with netcat

  • Create file
echo "{\"action\": \"CREATE_FILE\", \"fileName\": \"test.txt\"}" | nc -w1 -u 127.0.0.1 8080
  • Update file
echo "{\"action\": \"WRITE_INTO_FILE\", \"fileName\": \"test.txt\", \"content\": \"ala ma kota\"}" | nc -w1 -u 127.0.0.1 8080
  • Delete file
echo "{\"action\": \"DELETE_FILE\", \"fileName\": \"test.txt\"}" | nc -w1 -u 127.0.0.1 8080

With Node.js client app

client with netcat
Go to node-udp-communication/udp-client and exc commands:

npm i
npm start

(App send DETELE command after 10 sec)

Requirements

  • Node.js >= 10.0
  • npm >= 6.0

About

App for UDP communication which listens for datagrams.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published