Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Mosca error on publish - TypeError: Argument must be a string #66

Closed
ldstein opened this issue Oct 31, 2013 · 4 comments
Closed

Mosca error on publish - TypeError: Argument must be a string #66

ldstein opened this issue Oct 31, 2013 · 4 comments

Comments

@ldstein
Copy link
Contributor

ldstein commented Oct 31, 2013

Hi,

I'm currently testing Mosca and having a problem getting it to play nice with MongoDB.

On publishing to a subscribed client, the broker fails with the following error:

/usr/local/bin/node Server/app.js
Server ready

/home/leo/test/Server/node_modules/mosca/node_modules/mqtt/lib/generate.js:178
    length += Buffer.byteLength(payload);
                     ^
Published { cmd: 'publish',
  retain: false,
  qos: 0,
  dup: false,
  length: 28,
  topic: 'heartbeat',
  payload: <Buffer 74 69 6d 65 31 33 38 33 32 31 37 33 38 32 39 33 32> }
TypeError: Argument must be a string
    at Object.module.exports.publish (/home/leo/test/Server/node_modules/mosca/node_modules/mqtt/lib/generate.js:178:22)
    at MqttServerClient.eval [as publish] (eval at <anonymous> (/home/leo/test/Server/node_modules/mosca/node_modules/mqtt/lib/connection.js:58:29), <anonymous>:2:26)
    at Client.actualSend (/home/leo/test/Server/node_modules/mosca/lib/client.js:142:21)
    at Client.forward (/home/leo/test/Server/node_modules/mosca/lib/client.js:198:10)
    at Array.handler [as 0] (/home/leo/test/Server/node_modules/mosca/lib/client.js:347:10)
    at EventEmitter.TrieAscoltatore.publish (/home/leo/test/Server/node_modules/mosca/node_modules/ascoltatori/lib/trie_ascoltatore.js:52:11)
    at EventEmitter.newPublish (/home/leo/test/Server/node_modules/mosca/node_modules/ascoltatori/lib/abstract_ascoltatore.js:122:20)
    at /home/leo/test/Server/node_modules/mosca/node_modules/ascoltatori/lib/mongo_ascoltatore.js:202:29
    at process._tickCallback (node.js:415:13)

Process finished with exit code 8

Any pointers on where I might be going astray? To confirm, this error only occurs when configuring a MongoDB backend. I'm testing with Node v0.10.21 in both Windows x64 and Ubuntu - results are consistent.

Below are my server/client.js I am using for testing.

server.js:

var mosca = require('mosca');

var serverConfig =
{
    mosca:
    {
        port: 1883,
        backend:
        {
            type: 'mongo',
            uri: 'mongodb://localhost:27017/',
            db:  'mqtt',
            pubsubCollection: 'ascoltatori',
            mongo: {}
        }
    }
}

var server = new mosca.Server(serverConfig.mosca);

server.on('ready'    , function(){
    console.log('Server ready');
});

server.on('published', function(packet, client){
    console.log('Published', packet);
});

client.js:

var mqtt = require('mqtt');

var client = mqtt.createClient(1883, 'localhost');

var handleConnect = function()
{
    console.log('Connected to mqtt broker with ClientId:', client.options.clientId);
    client.on('message', handleMessage);

    client.subscribe('heartbeat');
    client.publish('heartbeat', 'time' + new Date().getTime());
}

var handleMessage = function(topic, message, packet)
{
    console.log('Received Message');
    console.log('topic:', topic, 'message', message);
}

client.on('connect', handleConnect);
@mcollina
Copy link
Collaborator

Nice catch!

The problem is that the Buffer coming out of MongoDB is not recognized as a node Buffer object by bops.is in https://github.com/adamvr/MQTT.js/blob/master/lib/generate.js#L176-L181.

I solved the very same problem there: https://github.com/mcollina/mosca/blob/master/lib/persistence/mongo.js#L249.

I think the best solution is to solve it inside the MongoAscoltatore (which is the component that handles MongoDB pub/sub): https://github.com/mcollina/ascoltatori.

Are you in very urgent need for this? Can you do a pull-request against ascoltatori?
I can fix this in a couple of days.

@mcollina
Copy link
Collaborator

Can you please try moscajs/ascoltatori#85 and see if it solves the problem for you?

@ldstein
Copy link
Contributor Author

ldstein commented Nov 1, 2013

Looking good, Mongodb is indeed working nicely now.

Many thanks!

@ldstein ldstein closed this as completed Nov 1, 2013
@mcollina
Copy link
Collaborator

mcollina commented Nov 1, 2013

Opening up, as that one is not release yet.

@mcollina mcollina reopened this Nov 1, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants