socket.io-stream in django (channels)

410 views Asked by At

I have a server which is used to download files. The server is written in nodejs and looks like this:

const express = require("express");
const http    = require("http");
const socketIO = require("socket.io");
const ss    = require("socket.io-stream");
const port  = 4001;
const index = require("./routes/index");
const fs    = require("fs");
var cors    = require('cors')

const app   = express();
app.use(cors());
app.use(index);

const server = http.createServer(app);
const io     = socketIO(server);

io.on("connection", (socket) => {
    console.log("New Client Connected");

    socket.on("download", (fileName) => {
        startStream(socket)(fileName);
    });

    socket.on("disconnect", () => {
        console.log("Client disconnected");
    });
});

const startStream = socket => fileName => {
    const file = `${__dirname}/public/${fileName}`;
    var stream = ss.createStream({allowHalfOpen: true});
    const { size } = fs.statSync(file);
    ss(socket).emit("filedownload", stream, {size} );
    
    var myFileStream = fs.createReadStream(file);
    myFileStream.pipe(stream);
}

server.listen(port, () => console.log(`Listening on port ${port}`));

It works great, but the problem is that it needs to be rewritten to Django - which is why i was looking at django channels. ' However I am not sure if Django channels can actually do something like this? The essential part here is socket.io-stream. How would I go about implementing this in Django Channels?

0

There are 0 answers