[cometD Javascript] Long polling fails in browser

21 views
Skip to first unread message

Rahmz

unread,
Jun 7, 2021, 6:42:16 PM6/7/21
to cometd-users
Hi All
does anyone has an example use of cometd javascipt with ES6 ?
I'm not sure if i got the setup right, my code is below. 
and do i need Jquery to use the javascript cometd ? I have a react application with this  
---------------------
require('cometd-nodejs-client').adapt();
var lib = require('cometd');

var cometd = new lib.CometD();

cometd.configure({
url: 'http://localhost:8082/myChannel/cometd',
logLevel: 'debug'
});

cometd.handshake(function(h) {
if (h.successful) {
console.log('handshake successful');

cometd.subscribe('/service/mp/pubsub_v1/api/subscribe', function(m) {
console.log('receiving a new message', m);
});
setInterval(function() {
cometd.publish('/service/mp/pubsub_v1/api/subscribe', {message: 'hi channel 1', when: Date.now()}, function() {
console.log('new message published');
});
}, 1500);
}
});
-----------------
it only work in Callback transport 
here is my console output 

17:12:44.816 Transport long-polling started waiting for message replies of request 1 : 10000 ms
cometd.js?a421:1480 17:12:44.822 Transport long-polling received error Failed to fetch undefined
cometd.js?a421:1480 17:12:44.822 Transport long-polling cancelled waiting for failed message replies
cometd.js?a421:1480 17:12:44.822 handleFailure window.XMLHttpRequest {status: 0, statusText: "Failed to fetch", readyState: 4, responseText: "", open: ƒ, …} [{…}] {reason: "Failed to fetch", exception: undefined, httpCode: 0, connectionType: "long-polling"}
cometd.js?a421:1480 17:12:44.823 Transport failure {cause: "failure", action: "handshake", transport: null} for {id: "2", successful: false, channel: "/meta/handshake", failure: {…}}
cometd.js?a421:1480 17:12:44.823 Transport websocket accept, supported: false
cometd.js?a421:1480 17:12:44.823 Transport long-polling -> callback-polling
cometd.js?a421:1480 17:12:44.823 Transport failure handling {cause: "failure", action: "handshake", transport: F, delay: 1000}
cometd.js?a421:1480 17:12:44.823 Function scheduled in 1000 ms, interval = 0 backoff = 2000 () => {
                _handshake(_handshakeProps, _handshakeCallback);
            }
cometd.js?a421:1480 17:12:45.826 Invoking timed function () => {
                _handshake(_handshakeProps, _handshakeCallback);
            }
cometd.js?a421:1480 17:12:45.826 New advice {timeout: 60000, interval: 0, maxInterval: 0}
cometd.js?a421:1480 17:12:45.826 Transport websocket accept, supported: false
cometd.js?a421:1480 17:12:45.826 Initial transport is callback-polling
cometd.js?a421:1480 

Simone Bordet

unread,
Jun 8, 2021, 4:09:47 AM6/8/21
to cometd-users
Hi,
You probably have a cross-domain setup.
Please read: https://docs.cometd.org/current/reference/#_javascript_transports_cross_origin.
Let us know if it worked for you.

--
Simone Bordet
----
http://cometd.org
http://webtide.com
Developer advice, training, services and support
from the Jetty & CometD experts.

Rahmz

unread,
Jun 9, 2021, 1:24:26 PM6/9/21
to cometd-users
yeah it turns out a CORS issue .... Cors = curse

Thanks for the help
Reply all
Reply to author
Forward
0 new messages