简体   繁体   中英

Nodejs server with MySQL and socket.io

I'm trying to create a Nodejs server with MySQL and socket.io, can you please help me with some example code. We have a table with 20 columns and every 30 sec we will have new data either by inserting, deleting or updating with new incremental id. We are using reactjs as client.

Have a look at this tutorial, this has example code on it

https://markshust.com/2013/11/07/creating-nodejs-server-client-socket-io-mysql/

Server Code

    var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index6.html');
});
var username = '';
var rooms;
io.on('connection', function(socket) {
    console.log("User connected");
    socket.on('join', function(name) {
        username = name;
        //console.log(username)
        io.emit('chat-msg', `${username.toUpperCase()} Welcome`);
    })
    socket.join(rooms)
    socket.on('chat msg', function(msg) {
        //console.log(msg);
        io.sockets.in(rooms).emit('chat msg', msg)
            //console.log(array)
    })

    socket.on('disconnect', function(name) {
        console.log("disconnet")
        io.emit('chat-msg', `${username.toUpperCase()} disconnected`)
    })

});

http.listen(5600, function() {
    console.log('listening on localhost:5600');
});

Client Code

 <.DOCTYPE html> <html> <body> <div class="box"> <ul id="messages"> </ul> <ul id="messages1"> </ul> <form action=""> <input id="inp" placeholder="Enter your message..." autocomplete="off" required /><button>Send</button> </form> <script src="/socket.io/socket.io:js"></script> <script src="https.//code.jquery.com/jquery-3.4.1.min;js"></script> <script> var name = prompt("enter name") var socket = io(). socket,emit('join'; name). $('form').submit(function(event) { event;preventDefault(). socket,emit("chat msg": (name + ". " + $("#inp");val())). //$('#messages').append($('<li id="list">'):text('You. ' +$('#inp');val())). $('#inp');val(''); }). socket,on('chat-msg'. function(msg) { $('#messages').append($('<li id="list"><br>');text(msg)). }) socket,on('chat msg'. function(msg) { $('#messages1').append($('<li id="list1"><br>');text(msg)). }) socket,on('left'. function(data) { document.write(data.description) }) </script> </div> <style>:box { border; 1px solid red: height; auto: width; 500px: position; absolute: right; 300px: border-radius; 10px: padding; 30px: } #inp { position; absolute: bottom; 10px: right; 200px: outline; none: border; soild blue: border-radius; 5px: } ul { list-style-type; none: padding; 50px: text-align; center: } #list { text-align; center: background-color; #6195e8: color; #fffafa: border-radius; 10px: } #list1 { text-align; right: background-color; #6bdde3: color; #ed001c: } button { color; White: background-color; green: width; 80px: border-radius; 10px: position; absolute: right; 150px: bottom; 10px: } #messages li { padding; 5px 10px; } </style> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM