Issue Client not receiving published messages from Nodejs server

308 views
Skip to first unread message

Navin Parray

unread,
Mar 6, 2012, 7:11:29 AM3/6/12
to Faye Users
Hello,

I'm having a problem where a message published on the server is not being received by a client that has subscribed. I have a nodejs server where I subscribe to the channel '/users/read' and I publish to the channel 'users/updated'. I also have a javascript web app where a client publishes to '/users/read' and subscribes to '/users/updated'. My console.log messages tell me that the subscriptions and publishing is being done in the correct order but the client never receives the message. That is, unless I kill the server using Ctrl+C. Then the client receives the message.

Here's the client code

FayeClient.subscribe("/users/updated/user1", function(data) {
    console.log('receiving user update: ' + new Date().getTime());
    console.log(data);
});

FayeClient.publish("/users/read", {"id":"user1"});
console.log('sent request: ' + new Date().getTime());

Here's the server code

fayeclient.subscribe('/users/read', function(data) {
    console.log('received request for user record: ' + new Date().getTime());
    fayeclient.publish('/users/updated/user1', {"id": "user1", "name": "user1 name"});
    console.log('sent user record: ' + new Date().getTime());
});

I've disabled, websocket using FayeClient.disable('websocket'); in the web app because I use nginx to proxy the call. The website runs at localhost:8080 (app.test.dev) and the nodejs app runs at localhost:3000 (app.test.dev/api). Also the nodejs server uses express and faye is attached to that.

I'm not sure if the issue is my coding of the faye subscriptions and publishings or if it is the web server config. 

Some additional info:

Here's the console.log output from the server when the app starts

clientid: 1cwd0qy1vjg8hi1kaypf71ey2bv3
subscribed: 1cwd0qy1vjg8hi1kaypf71ey2bv3 : /users/read

When I visit the web app in the browser, the server console.log output is 

clientid: 1k5pwpq0aoaige0r1fzuw1w4g9h0
published: 1k5pwpq0aoaige0r1fzuw1w4g9h0 : /users/read
subscribed: 1k5pwpq0aoaige0r1fzuw1w4g9h0 : /users/updated/user1
received request for user record: 1331033657508 (this is a timestamp)
published: 1cwd0qy1vjg8hi1kaypf71ey2bv3 : /users/updated/user1
sent user record: 1331033657510 (this is a timestamp)

The output from in the browsers console is 

sent request: 1331033657488 (this is a timestamp)




James Coglan

unread,
Mar 6, 2012, 7:25:03 AM3/6/12
to faye-...@googlegroups.com
On 6 March 2012 12:11, Navin Parray <navin...@gmail.com> wrote:
Here's the client code

FayeClient.subscribe("/users/updated/user1", function(data) {
    console.log('receiving user update: ' + new Date().getTime());
    console.log(data);
});

FayeClient.publish("/users/read", {"id":"user1"});
console.log('sent request: ' + new Date().getTime());

The problem might be that you're not waiting for the subscription to be activated before publishing. Try this:

var sub = FayeClient.subscribe("/users/updated/user1", function(data) {
    console.log('receiving user update: ' + new Date().getTime());
    console.log(data);
});

sub.callback(function() {
    FayeClient.publish("/users/read", {"id":"user1"});
    console.log('sent request: ' + new Date().getTime());
});

Navin Parray

unread,
Mar 6, 2012, 8:24:39 AM3/6/12
to faye-...@googlegroups.com
I tried this change and I've got the same problem. 

I do notice that GET request that is supposed to receive the message


has a header with this


The HTTP/1.1 makes me think the issue is with the proxying on the nginx server and not the faye code.

James Coglan

unread,
Mar 6, 2012, 8:51:35 AM3/6/12
to faye-...@googlegroups.com
On 6 March 2012 13:24, Navin Parray <navin...@gmail.com> wrote:

The HTTP/1.1 makes me think the issue is with the proxying on the nginx server and not the faye code.

It's not the HTTP/1.1. The fact the path contains a client ID means this is an EventSource request, which nginx might be blocking. Try doing this:

FayeClient.disable('eventsource'); 

Navin Parray

unread,
Mar 6, 2012, 9:53:12 AM3/6/12
to faye-...@googlegroups.com
That worked! I'm guessing the EventSource request has to do with WebSockets. I had assumed the disable('websockets') call would have handled that and long-polling would have been used throughout the system.

Thank you for your help.

James Coglan

unread,
Mar 6, 2012, 9:59:17 AM3/6/12
to faye-...@googlegroups.com
On 6 March 2012 14:53, Navin Parray <navin...@gmail.com> wrote:
That worked! I'm guessing the EventSource request has to do with WebSockets. I had assumed the disable('websockets') call would have handled that and long-polling would have been used throughout the system.

EventSource and WebSocket are two different transports. The thing about EventSource is it's very hard to detect errors in it, since it just keeps trying to reconnect for you. Could you run the following in a browser against your server and see what happens?

var es = new EventSource('http://app.test.dev/faye/1rp');
es.onopen = function() { console.log('OPEN') };
es.onclose = function() { console.log('CLOSE') };
es.onerror = function() { console.log('ERROR') };

Navin Parray

unread,
Mar 6, 2012, 10:49:37 AM3/6/12
to faye-...@googlegroups.com
I added the code and removed the disable on websocket and eventsource, I got the following error on the node server

2012-03-06 11:35:07 [ERROR] [Faye.NodeAdapter] Unexpected token u
Backtrace:
SyntaxError: Unexpected token u
    at Object.parse (native)
    at Object._callWithParams (/var/apps/modulartest/node_modules/faye/faye-node.js:2228:26)
    at Object.handle (/var/apps/modulartest/node_modules/faye/faye-node.js:2146:19)
    at HTTPServer.<anonymous> (/var/apps/modulartest/node_modules/faye/faye-node.js:2119:52)
    at HTTPServer.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1491:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1387:22)
    at TCP.onread (net.js:354:27)

If I kill the server, I get OPEN and ERROR in the browser console and on restarting the server I get another ERROR output in the console.

With the disable websocket and eventsource in place, I get errors neither on the server nor browser.

James Coglan

unread,
Mar 12, 2012, 12:51:46 PM3/12/12
to faye-...@googlegroups.com
On 6 March 2012 15:49, Navin Parray <navin...@gmail.com> wrote:
I added the code and removed the disable on websocket and eventsource, I got the following error on the node server

2012-03-06 11:35:07 [ERROR] [Faye.NodeAdapter] Unexpected token u
Backtrace:
SyntaxError: Unexpected token u
    at Object.parse (native)
    at Object._callWithParams (/var/apps/modulartest/node_modules/faye/faye-node.js:2228:26)
    at Object.handle (/var/apps/modulartest/node_modules/faye/faye-node.js:2146:19)
    at HTTPServer.<anonymous> (/var/apps/modulartest/node_modules/faye/faye-node.js:2119:52)
    at HTTPServer.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1491:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1387:22)
    at TCP.onread (net.js:354:27)

If I kill the server, I get OPEN and ERROR in the browser console and on restarting the server I get another ERROR output in the console.

Sorry, I just meant to run the EventSource code I gave you without going through Faye at all -- just boot the Faye server, then run that code in the browser console. What messages do you see, and are there any server-side errors?

高财吴

unread,
May 16, 2013, 4:04:15 AM5/16/13
to faye-...@googlegroups.com
The below code need run in the client or server.  where?


FayeClient.disable('eventsource');

thanks.

在 2012年3月6日星期二UTC+8下午9时51分35秒,James Coglan写道:

James Coglan

unread,
May 16, 2013, 4:07:32 AM5/16/13
to faye-...@googlegroups.com
On 16 May 2013 09:04, 高财吴 <long7...@gmail.com> wrote:
The below code need run in the client or server.  where?


FayeClient.disable('eventsource'); 

This is on the client. 
Reply all
Reply to author
Forward
0 new messages