meteor icon indicating copy to clipboard operation
meteor copied to clipboard

SockJS/JSON.parse conflict can break Meteor application

Open bbbmmmlll opened this issue 13 years ago • 1 comments

The xRTML framework (http://www.xrtml.org/) will break Meteor after a Meteor restart. This is with Meteor 0.5.4 and XRTML 3.0.

Initially the Meteor application and XRTML work correctly with Meteor's SockJS handling XHR events. Once Meteor restarts, xRTML's SockJS handlers take over.

xRTML parses messages at the SockJS.dispatchMessage() call so when the messages eventually get to Meteor they have already been converted into objects and Meteor discards them after trying to JSON.parse() them. Recovery requires the web page to be reloaded in the browser.

The browser (Chrome 24.0.1312.56/IE10/Safari 6.0.2) console will show errors like:

DEBUG: malformed welcome packet Object
DEBUG: invalid welcome packet
discarding message with invalid JSON Object

A working call stack:

(anonymous function) livedata_connection.js:171
(anonymous function) stream_client.js:355
_.each._.forEach underscore.js:79
self.socket.onmessage stream_client.js:354
REventTarget.dispatchEvent sockjs-0.3.4.js:86
SockJS._dispatchMessage sockjs-0.3.4.js:1039
SockJS._didMessage sockjs-0.3.4.js:1097
poll.onmessage sockjs-0.3.4.js:2196
REventTarget.dispatchEvent sockjs-0.3.4.js:86
that.xo.onchunk sockjs-0.3.4.js:2356
EventEmitter.emit sockjs-0.3.4.js:150
that.xhr.onreadystatechange sockjs-0.3.4.js:807

The broken Meteor call stack:

(anonymous function) livedata_connection.js:173
(anonymous function) stream_client.js:355
_.each._.forEach underscore.js:79
self.socket.onmessage stream_client.js:354
REventTarget.dispatchEvent IbtRealTimeSJCore.js:1
SockJS._dispatchMessage IbtRealTimeSJCore.js:1
SockJS._didMessage IbtRealTimeSJCore.js:1
poll.onmessage IbtRealTimeSJCore.js:1
REventTarget.dispatchEvent IbtRealTimeSJCore.js:1
that.xo.onchunk IbtRealTimeSJCore.js:1
EventEmitter.emit IbtRealTimeSJCore.js:1
that.xhr.onreadystatechange IbtRealTimeSJCore.js:1

This behavior can be reproduced with the following steps:

  • create an empty Meteor project
  • create a text.html file with the following lines:
<head>
  <script type="text/javascript" src="http://code.xrtml.org/xrtml-3.0.0.js"></script>
  <script type="text/javascript">
      xRTML.ConnectionManager.create({
        id:'ACME_connection',
        appKey:'XRTMLO',
        authToken:'a5pmrzx3u5',
        url:'http://ortc-developers.realtime.co/server/2.1',
        channels:[{name:'acme_channel_a5pmrzx3u5'}]
      });
    });
  </script>
</head>
  • start project
  • go to project with web browser and open console (it's a blank page)
  • restart project on server which will result in browser console error messages and the Meteor page becoming non-reactive

Note: The xRTML connection settings are from one of their online demos and could become invalid at any moment. You can get free credentials by signing up as a developer.

I don't know if Meteor or XRTML or both are doing something incorrect with regards to SockJS and it's seems like an obscure bug, but it's easy to make Meteor handle this situation.

bbbmmmlll avatar Jan 29 '13 21:01 bbbmmmlll

Brian, thanks for tracking this down and figuring out what's going on.

So if I understand right, xRTML is shipping a private version of SockJS that has a different, proprietary API (returning objects rather than strings?) I sympathize with the difficulty this causes but it seems like the problem here is with xRTML, since it would break any other application that uses SockJS (not just Meteor), is that correct?

I'm open to doing whatever it takes to make Meteor easy to use, especially when it comes to interop with other tools (and the short patch you sent is definitely in that spirit), but my concern is that we would be stuck supporting this forever (if we broke it in Meteor version 12.3 then people would blame us breaking their xRTML apps that they wrote ten years ago :)) It also seems like it would be better for the web (and for xRTML's own interop) to encourage xRTML to find a different way to do this.

I am open to being persuaded otherwise though!

gschmidt avatar Feb 04 '13 20:02 gschmidt