Skip to content

Can't sent byte[] from the client to the SignalR hub #53824

@jaywork2020

Description

@jaywork2020

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Unable to send byte[] from a client using jacvascript to the SignalR hub.

Here is the javascript code that does the recording and then when the sendButton is called, the byte array is send to the signalR hub using the upload function.

const connection = new signalR.HubConnectionBuilder().withUrl("/hub").build();

let audioRecorder;
let data = [];

navigator.mediaDevices.getUserMedia({ audio: true })
    .then(stream => {

        audioRecorder = new MediaRecorder(stream);

        audioRecorder.addEventListener('dataavailable', e => {
            data.push(e.data);
        });

        startButton.addEventListener('click', () => {
            audioRecorder.start();
        });

        stopButton.addEventListener('click', () => {
            audioRecorder.stop();
        });

        sendButton.addEventListener('click', async () => {

            const blobObj = new Blob(audioChunks, { type: 'audio/webm' });

            var bytes = await blobObj.arrayBuffer();

            const subject = new signalR.Subject();
            subject.next(bytes)
            connection.invoke('upload', 'filename', subject);
            subject.complete();
        });
    })

The SignalR hub I added this code

public async Task Send(string filename, IAsyncEnumerable<byte> stream)
{
    using var memoryStream = new MemoryStream();

    await foreach (var item in stream)
    {
        memoryStream.WriteByte(item);
    }
}

But nothing is being sent.

I tried to use byte[], IList<byte> and ArrayList instead like this

    public Task Upload(string filename, IList<byte> stream)
    {
        using var memoryStream = new MemoryStream();

        return Task.CompletedTask;
    }

But that throws a Javascript error in the console.

Error: Failed to invoke 'Upload' due to an error on the server. InvalidDataException: Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked.

Expected Behavior

I am expecting sending a subject with a byte array will send the byte[] to the stream property. The Send function gets called, but the stream is always empty.

public async Task Send(string filename, IAsyncEnumerable<byte> stream)
{
    using var memoryStream = new MemoryStream();

    await foreach (var item in stream)
    {
        memoryStream.WriteByte(item);
    }
}

Steps To Reproduce

No response

Exceptions (if any)

Error: Failed to invoke 'Upload' due to an error on the server. InvalidDataException: Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked.

.NET Version

8.0.101

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.area-signalrIncludes: SignalR clients and servers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions