Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Send a message from client to server (Websocket)

I have a .php file, where I have 2 buttons, one to upload a file and the other to read it line by line.

(Bellow the image of buttons and the code)

Buttons

HTML

<div style="font-size:20px;" class="sub-title">File Upload</div>                                                                                        
<input class="buttonupload" type="file" name="fileToUpload" id="fileToUpload">                                                                                                                                   
<button id="uploadFileButton">Start</button>

Script to read the file line by line with delay

 // Read file line by line with delay
    function readFile(file, onLoadCallback){

        var reader = new FileReader();
        reader.onload = onLoadCallback;
        reader.readAsText(file);
    }
    
    $('#uploadFileButton').on('click', function(e){
        readFile(document.getElementById('fileToUpload').files[0], function(e) {  
            var content = e.target.result;
            var fileContentArray = content.split(/\r\n|\n/);
            let index = 0;
            const interval = setInterval(function() { 
            console.log(fileContentArray[index]);
            ++index;
        }, 1000)
        });
    });

I have a websocket server too.

Websocket.js

const WebSocket = require("ws");       
const server = new WebSocket.Server({  port: 8083 });                                         

let sockets = [];                           

server.on('connection', function(socket) {  
    sockets.push(socket);                   
    console.log("Connection established");

    socket.on('message', function(msg) {    
    console.log('Cliente: %s', msg);    
    sockets.forEach(s => s.send(msg.toString()));
    });

   socket.on('close', function() {       
   sockets = sockets.filter(s => s !== socket);  
   });
});

And here is the .php (websocket)

  function connect() {
        console.log("Trying to connect with websocket...");
        //const socket = new WebSocket("ws://" + ip + ":5656");
        const socket = new WebSocket('ws://localhost:8083');
            

My question is: How can I send the content of the file to the server?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!