Sobes.tech
Junior — Middle

How to implement data exchange between two separate servers or machines?

sobes.tech AI

Answer from AI

Data exchange between two separate servers or machines can be implemented in several ways, depending on requirements and conditions:

  • HTTP/HTTPS requests: One server acts as a client, sending requests to another server, which responds. Used for REST API.
  • SSH and SCP: For secure file transfer and remote command execution.
  • FTP/SFTP: For file transfer between machines.
  • Sockets (TCP/UDP): For real-time two-way data exchange.
  • Message brokers (RabbitMQ, Kafka): For asynchronous message exchange.
  • Shared file system access: NFS, SMB for shared data usage.

Example of simple HTTP exchange (Node.js):

// Sender server
const http = require('http');
const data = JSON.stringify({ message: 'Hello, server 2!' });

const options = {
  hostname: 'server2.example.com',
  port: 80,
  path: '/receive',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }
};

const req = http.request(options, res => {
  console.log(`Response status: ${res.statusCode}`);
});

req.write(data);
req.end();

This approach is universal and widely used.