0

I'm creating a chat app with socket.io, i need to know, how i can broadcast images as a user to other users in the "chat room" without the need to first save the picture in a directory. The main goal is to open the image from a "file input" and be able to send the file ('picture') to other users so they can view It in the chat

B. J
  • 11
  • 2
  • You can broadcast the image as a `Base64` string. See the following answer https://stackoverflow.com/a/8499716/1429618 – Daniel Oct 05 '21 at 10:24

1 Answers1

0

Fundamentally, use FileReader to load the file as a Buffer, then send it. When receiving an image, put its blob into createObjectURL and create an image tag.

Basic Example (View Sandbox)

<input type="file" id="img" onchange="setImgSrc(this)" accept="image/*" />
<input type="submit" onclick="submitImg()" />
<div></div>
<!-- OUTPUT DIV -->
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect()

  var src

  var setImgSrc = (elm) => {
    var fr = new FileReader()
    fr.onload = () => (src = fr.result)
    fr.readAsArrayBuffer(elm.files[0])
  }

  var submitImg = () => socket.emit('submitImg', src)

  socket.on('sentImg', (src) => {
    // Create Img...
    var img = document.createElement('img')
    img.src = (window.URL || window.webkitURL).createObjectURL(
      new Blob([src], {
        type: 'image/png'
      })
    )
    img.width = 200
    img.height = 200
    document.querySelector('div').append(img)
  })
</script>
const express = require('express')
const app = express()
const http = require('http').Server(app)
app.use(express.static('src/client'))
const io = require('socket.io')(http)

io.on('connection', (socket) => {
  console.log('Client connected')

  socket.on('submitImg', (src) => {
    console.log('Client sent image')

    //Client submit an image
    io.emit('sentImg', src) //the server send the image src to all clients
  })
})

const port = 8080
http.listen(port, () => {
  console.log('Server Running on Port ' + port)
})
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Thank you very much, you really helped me. I am grateful that you took your time and helped me! – B. J Oct 05 '21 at 10:47
  • I got one more question for you, answer only if you have time for It, I wonder if you know how to clean the value so everytime I send a picture, everything resets, because right now if you choose 1 file you can send It unlimited times by clicking submit btn – B. J Oct 05 '21 at 14:20
  • wrap the form items in a form then do a [reset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset), `() => {form.reset();socket.emit('submitImg', src)}`. I said fundamentally, because there is other things you need to add, like validation, size and type of file, probably client-side image resize using canvas, authentication on the socket.. I did something similar a while back but it shared torrent hashes, maybe is of some interest https://codesandbox.io/s/clipseed-hfhee – Lawrence Cherone Oct 05 '21 at 17:57
  • thank you :D, it helped alot – B. J Oct 07 '21 at 15:18