Node application Primus client's emitted data not received by Primus server

322 Views Asked by At

I have an AngularJS application as a Primus client and it is able to connect to the Primus server and emit to the Primus server too without any issues.

I built a new NodeJS application but acts as a Primus client. It's on the same machine as my Primus server. This is the code that I use to connect to the Primus server. It is able to connect to it as I see the hash generated when it connects.

var http   = require('http'),
    server = http.createServer(),
    Primus = require('primus'),
    primus = new Primus(server, { transformer : 'sockjs' }),
    Socket = primus.Socket;

var client = new Socket('http://192.168.1.50:7777');

// prmius.use('emit', require('primus-emit'));
// client.use('emit', require('primus-emit'));

// primus.emit('motion-sensor-on', { isActive : true, area : 'living-room', src : 'node' });
client.emit('motion-sensor-on', { isActive : true, area : 'living-room', src : 'node' });

The client.emit gets executed but the Primus server doesn't receive it. It's on the same machine. I'm going to mention it again that when I start the node application, it is able to connect to the primus server. Previously, I first tried primus.emit but my primus server is unable to receive it too.

1

There are 1 best solutions below

0
On

I finally got it working!!!

Well, I just added a new listener and event name 'data'. Looks like if you are emitting from the server as client using socket or client.write, you should not specify an event name. So my latest working code on the server acting as a client is

client.write({ isActive : true, area : 'living-room', src : 'node' });

then on the primus server, the code is

spark.on('data', function(data) {
    console.log(spark.id, 'received message:', data);
    primus.write(data);
});

I'm honestly not sure why custom event names works when using it in browsers but not when you are sending from inside your NodeJS app. That sucks! Maybe, I missed a code. Not sure! Anyways, I am now able to see the motion sensor changing from my browser and phone.