M
M
Max2019-10-23 23:45:01
Node.js
Max, 2019-10-23 23:45:01

How to implement receiving commands to an application on node js on the server?

Hello!
There is a node js application written in electron for drawing the interface and managing the application itself. I need to host this application on a server and accept commands from another server.
The server where the application is hosted on ubuntu with gui.
The application itself must always be running, accepting and responding to commands from an external server. The database is not being used.
How are such tasks usually implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shvets, 2019-10-24
@maximrabotaet

Yes, whatever you like, in this case, the easiest way is to open a web socket. Just make the authorization simple, jwt is just right.

M
Max, 2019-10-29
@maximrabotaet

A working example, MB is useful to someone, because. I found very few solutions in php.
php client:

<?
require __DIR__ . '/vendor/autoload.php';

use Ahc\Jwt\JWT;
use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;

$secret = 'shecret';
$host = 'http://localhost';
$port = 3000;
$maxAge = 600;

$payload = [
    'data' => '123',
];
$jwt = new JWT($secret, 'HS256', $maxAge);
$token = $jwt->encode($payload);

$client = new Client(new Version2X($host .':'. $port));

$client->initialize();

$client->emit('authenticate', ['token' => $token]);

$isAuthenticated = formatResponse($client->read());

if('authenticated' !== $isAuthenticated['event']){

    $client->close();
}

$data = [
    'data' => 'from client'
];
$token = $jwt->encode($data);

$client->emit('action', ['token' => $token]);

$response = formatResponse($client->read());

if($response['data']){

    $response = ($jwt->decode($response['data']->token))['data'];
}

$client->close();

echo '<pre>';
print_r($response);
echo '</pre>';


function formatResponse($response){

    $response = json_decode(preg_replace('/^\d*/i', '', $response));

    return $response ? [
            'event' => $response[0],
            'data' => $response[1],
        ] : false;
}

nodejs server:
require("@babel/register");
const
    socketIO = require('socket.io'),
    jwt = require('jsonwebtoken'),
    socketioJwt = require('socketio-jwt'),
    port = 3000,
    server = socketIO.listen(port),
    secret = 'shecret',
    maxAge = 600;

server
    .on(
        'connection',
        socketioJwt.authorize({
            secret: secret,
            timeout: 10000,
            callback: false
        }
    ))
    .on('connection', socket => {

        console.log('on connection to server (server)');
    })
    .on('authenticated', socket => {

        console.log('on authenticated to server (server), socket.decoded_token', socket.decoded_token);

        socket.on('action', data => {

            console.log('on action (server)', data);

            if(data && 'token' in data){

                let decoded = null;

                try {

                    decoded = jwt.verify(data.token, secret);

                } catch (err) {

                    console.log(err);
                }

                if(decoded){

                    console.log(decoded);

                    const token = jwt.sign(
                        {
                            exp: Math.floor(Date.now() / 1000) + maxAge,
                            data: 'from server'
                        },
                        secret);

                    socket.emit('action', {token: token});

                } else {

                    socket.disconnect(true);
                }
            }
        })
    })
;

Packages:
// composer.json
    "adhocore/jwt": "^0.1.0",
    "wisembly/elephant.io": "^3.3"

// package.json
    "jsonwebtoken": "^8.5.1",
    "socket.io": "^2.3.0",
    "socketio-jwt": "^4.5.0"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question