Russill Glover
Russill Glover

Reputation: 71

Sending UDP packets in Electron through Node.js datagram

I'm trying to send a udp packet using the node.js dgram package from an electron app. The send method calls for a Buffer, Uint8Array, or string. I'm getting a type error when I try to use a Buffer or UintArray however. The exact error is: TypeError: First argument must be a buffer or a string.

I can see in the Node.js dgram code that it is checking for a buffer using !(buffer instanceof Buffer . If I try using instanceof on the buffer in my code it just returns object as the type, while if I try 'Buffer.isBuffer()' it returns true. I've tried all the methods that node provides for creating buffers buffer with no results. Send does accept strings but then I am running into all sorts of issues with encoding, and that feels kinda of hacky.

It looks like electron is running node 7.4.0.

I see my options from here as:

  1. Edit the node.js code
  2. Somehow override what instanceof returns for my buffer object
  3. Figure out a solution for properly encoding the Buffer as a string.

Any ideas?

Upvotes: 2

Views: 5427

Answers (1)

Russill Glover
Russill Glover

Reputation: 71

To get this to work I just had to point my global Buffer value to the node buffer module as so: const Buffer = window.require('buffer').Buffer

Upvotes: 5

Related Questions