Code Monkey home page Code Monkey logo

node-udp-communication's Introduction

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

node-udp-communication's People

Contributors

bartekck avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.