Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not clear on how server can receive binary from client #4

Closed
scottstensland opened this issue Apr 8, 2014 · 4 comments
Closed

not clear on how server can receive binary from client #4

scottstensland opened this issue Apr 8, 2014 · 4 comments
Labels

Comments

@scottstensland
Copy link

I would like to use your library to allow my browser client side to send to node-js server side binary data in form of a typed array such as Uint8Array. I am successfully sending normal text from client to server. When attemping binary, here is my client side send

socket.binaryType = 'arraybuffer';
socket.send(big_monster_data);

and here is my nodejs-websocket server side which is NOT correctly receiving the client sent data

var server = ws.createServer(function (connection_request) {

// here is text which is received just fine
connection_request.on("text", function (received_data) {
        console.log("Received text format : " + received_data);
        connection_request.sendText(received_data.toUpperCase()+" OK");
    });

// here is attempt at binary which is not working
connection_request.on("binary", function (received_data) {

// below gets printed YET length is undefined
console.log("Received binary format of length ", received_data.length);

    if (received_data instanceof ArrayBuffer) {

        // this is NOT getting reached
        console.log("OK instanceof ArrayBuffer");
    }

    // HERE I want to do something with received arraybuffer
}

}

thank you for your time

cheers, Scott Stensland

@sitegui
Copy link
Owner

sitegui commented Apr 8, 2014

Hi,

The 'binary' event returns a readable stream, see https://github.com/sitegui/nodejs-websocket#event-binaryinstream

That means you should write:

// Listen for binary event
conn.on("binary", function (inStream) {
    // Collect all the data in a buffer
    var data = new Buffer(0)

    // Get all frames of binary data and add to the buffer
    inStream.on("readable", function () {
        var newData = inStream.read()
        if (newData)
            data = Buffer.concat([data, newData], data.length+newData.length)
    })

    // Done, process the big data
    inStream.on("end", function () {
        process_my_data(data)
    })
})

I haven't noticed an example of this use case is missing...

I have designed this way because a binary data can be very long and the client can split it in different websocket frames.

A security hint: put a limit in the Buffer above, because it is stored in the process memory and someone could send you tons of data and make node run out of memory. This would collapse your program entirely ;) (this module does this internally, see https://github.com/sitegui/nodejs-websocket#wssetmaxbufferlengthbytes)

Hope it helps,
Guilherme

@scottstensland
Copy link
Author

Superb !!! works like a charm - merci beaucoup

@nazar-pc
Copy link
Contributor

@sitegui, I'd add this to readme, since I'm working with JS every day, but not with Node.js and having need for very simple WebSockets server it was not clear for me how to get binary data as well.

@sitegui
Copy link
Owner

sitegui commented Nov 22, 2015

Hi @nazar-pc, that's a valid point.

I'm open to a pull request if you like, otherwise I can manage to do this by the middle of the week.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants