user375566
user375566

Reputation:

node-websocket-server TypeError: Argument must be a string

I'm tailing a /var/log/logfile on ubuntu, and getting a TypeError. Works on my local Mac machine /var/log/system.log.

Command:

sudo node server.js /var/log/logfile (must be sudo to view this log file)

Error:

/npm/node_modules/node-websocket-server/lib/ws/connection.js:290
    var byteLen = Buffer.byteLength(data, 'utf8'),
                         ^
TypeError: Argument must be a string
    at Connection.<anonymous> (/npm/node_modules/node-websocket-server/lib/ws/connection.js:290:26)
    at clientWrite (//npm/node_modules/node-websocket-server/lib/ws/server.js:34:12)
    at /npm/node_modules/node-websocket-server/lib/ws/server.js:99:7
    at Manager.forEach (/npm/node_modules/node-websocket-server/lib/ws/manager.js:125:14)
    at Server.broadcast (npm/node_modules/node-websocket-server/lib/ws/server.js:98:13)
    at Socket.<anonymous> (/npm/server.js:63:11)
    at Socket.emit (events.js:64:17)
    at Socket._onReadable (net.js:677:14)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

Code:

/*
 * server.js
 */

var util   = require('util');
var sys = require("sys");
var ws = require("/npm/node_modules/node-websocket-server");
var spawn = require('child_process').spawn;

var filename = process.ARGV[2];

if (!filename)
  return sys.puts("Usage: node <server.js> <filename>");

var server = ws.createServer({debug: true});

/*
 * After server comes up
 */
server.addListener("listening", function() {
    sys.log("Listening for connections on localhost:9997");
});

/*
 * Define any port
 */
server.listen(9997);

var tail = spawn("tail", ["-f", filename]);
sys.puts("start tailing");

tail.stdout.on("data", function (data) {
    /* 
     * Send data
     */
    server.broadcast(data);
    console.log('' + data);
});

Upvotes: 1

Views: 1133

Answers (1)

user721340
user721340

Reputation:

At present node-websocket-server only accepts data in the form of Strings, not Buffers. Buffers may be accepted in the future, this is pretty much due to the limitation of framing and the fact that I don't want to be doing large amounts of buffer copying. (It seems irresponsible to write out just 0xFF or 0x00 to a socket).

Also, try doing:

var tail = spawn('tail', ['-f', filename]);

tail.stdout.setEncoding('utf8')

As the streams of stdout and stderr default to no encoding, meaning that the data event emits Buffer objects. As to why this works on your mac but fails on ubuntu, I'm not sure, but doing a quick test just a moment ago, I have both ubuntu and mac giving me buffer objects if I didn't specifically set the encoding of the streams.

Upvotes: 3

Related Questions