O
O
Ostap Likhodeev2019-09-25 19:23:16
Node.js
Ostap Likhodeev, 2019-09-25 19:23:16

How to query db from websocket server on node.js?

How to execute a db query when client disconnects from websocket in node.js ?
Disconnect Event:

socket.on('disconnect', () => {
        clients.splice(clients.indexOf(socket.id), 1);
        socket.emit({"discconnected":userId,"roomOut":room});
        console.log({"discconnected":userId,"roomOut":room});
    })
});

In PHP, this is how I did it through AJAX:
session_start();
require_once('../system/Config.php');


if($_POST['GetOutRoom']) {
    $id=$_POST['GetOutRoom'];    
    $updata1 = "UPDATE `user` SET `ontime`=0 WHERE `uid` = $id";
    $up1 = $pdo->prepare("$updata1");
    $up1->execute();
}

How to perform the same on node server when disconnected from websocket?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
McBernar, 2019-09-25
@Ostap74

What do you mean? Connect the MySQL driver to the project and make a request in the callback.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question