Datatables and Websockets

Datatables and Websockets

vasalosvasalos Posts: 2Questions: 0Answers: 0

I made a lot of researching about websockets on Datatables and unfortunately my research was pretty poor (or I am bad programmer on Javascript :smile: ).
I have a MySQL database with about 500.000 rows. So far, the best way I have successfully return my data back, is ServerSide with the SSP class. No matters how many results I have into my database. Have in mind that all these data are updating every about 10 seconds. With a simple ajax query every 5 seconds I could have the updated data on my client side.
The ajax return can give me TotalRecords, TotalDisplayRecords etc. So I came with the idea to have some resources savings from the serverside and create websockets. First of all, it is impossible to push 500.000 rows on initial call to first build my datatable. Second there is no way that you can send only updates through ajax ot websockets, as the updates will be again as much as the totalrecords. So my question is, how can I implement, initial start through websockets and have the functionality of serverside (SSP class) script?
PS: I am very curious why datatables are not implementing a websocket plugin or API, since, pretty much people are already trying to do so?

Regards,
Vasili

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    For what you describe I'd try to combine the two approaches:

    1. Use server-side processing (you can do that over the socket, or with Ajax calls as you wish).
    2. When sending the server-side processing request for data, subscribe an event handler for the rows that are selected (with PG you could use a notify - not sure if MySQL has something similar). Then if any of those change or are deleted tell the client to request a redraw.

    PS: I am very curious why datatables are not implementing a websocket plugin or API, since, pretty much people are already trying to do so?

    Because the implementations would be very case specific. You can use ajax to pipe the Ajax communications DataTables uses over a socket, but much more than that and it is server specific, and DataTables core doesn't currently provide server-side scripts (the demo server-side processing script is just that, a demo).

    Allan

  • vasalosvasalos Posts: 2Questions: 0Answers: 0

    Hi allan,
    Thanks for your quick reply. I ve tried do something like this:

    var table = $("#results").DataTable({
                "iDisplayLength": 10,
                "processing": true,
                "deferRender": true,
                "serverSide": true,
                "data": function() { subscribews(); },
                "aLengthMenu": [[5, 10, 20, 30, 50], [5, 10, 20, 30, 50]],      
                "dataType": "json",
                "order": [[ 6, "desc" ]], 
                            ............
    
    function subscribe(){
    
    let ws = new WebSocket("wss://xxx.xxx.xxx.xxx");
    ws.onopen = function() {
        console.log("WS connected...");
    };
    ws.onmessage = function(evt) {
        try {
            console.debug(evt.data);
            return evt.data.data;
        } catch (e) {
            console.log('Unknown message: ', e);
        }
    }
    

    But, I have not have to send all my 300K rows, just the first 10 of my 1st page, as per serverside request sent. As I said before, my data are changing into the DB very very quick( very often deletions, updates on the selected rows), so there is no reason to send all data as updates, just a simple reload of the first data.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    so there is no reason to send all data as updates, just a simple reload of the first data.

    Agreed - so the server-side needs to know what data is currently being displayed, so it can then stream updates for those records. So its going to be heavily dependent on whatever solution you use on the server-side.

    Allan

This discussion has been closed.