title: Right way to implement file upload for WebSocket chat application
body: <p>I have a chat application, backend is written with help of <a href="https://socket.io/" rel="nofollow noreferrer">Socket.io</a> (based on <a href="https://nodejs.org" rel="nofollow noreferrer">Node.js</a> server), and client is pure vanilla JavaScript, with <a href="https://jwt.io/" rel="nofollow noreferrer">JWT</a> token generation from PHP, chat loads all data on load from REST API written on PHP also, and i am stuck with file upload.</p>
  
  <p>I have two variants, first is REST API, just send data with help of <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest" rel="nofollow noreferrer">XMLHTTPRequest</a>, and i already implemented this, it's a simple <a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData" rel="nofollow noreferrer">FormData</a> POST to REST, 
  it works fine, but i am not sure of "rightness" of this solution, because for now REST have only GET routes for simple users (there is admin routes also) and by design of my project, it's set to get data for user chat, and its looks not right for me to make POST route for them, but what worries me most is what i already have another mechanism/solution where user sending their messages, it is socket.io-node.js server, with all permission checks (does this user have permission to join this chat, send information to it, and so on), so REST will take part of Socket.io task. Also beside role distribution problem, REST solution is also a logic duplication, where all permission checks written on Node.js side, must be duplicated on PHP side. As a result i already started to rewrite file sending to Node.js via Socket.io. Socket.io from version 1 is accepting binary data, so it's was pretty easy, i just sending a <a href="https://developer.mozilla.org/en/docs/Web/API/File" rel="nofollow noreferrer">File</a> and Node.js gets <a href="https://nodejs.org/api/buffer.html" rel="nofollow noreferrer">Buffer</a>, which is easily stored to filesystem like this</p>
  
  <pre><code>require('fs').writeFileSync(filePath, Buffer);
  </code></pre>
  
  <p>but here i have problem what i don't have upload progress, and most importantly despite all projects part are now perfectly distributed by their roles (client is sending data to socket.io, and reads from rest), and i have no role duplication, i am still not 100% sure i solved this problem right.</p>
  
  <p>So question is, how and where would you send your files, in case of your chat application. </p>