websockets, dart javascript interop problem

181 views
Skip to first unread message

peter ferko

unread,
Feb 17, 2014, 6:19:39 AM2/17/14
to w...@dartlang.org
Hi all, we are building realtime application we use spring4 backend and like to use dart in frontend. Spring4 support websockets through sock_js and stomp. There are client dart libraries of both: sock-js-dart-client (https://github.com/nelsonsilva/sockjs-dart-client
) and stomp_dart  (https://github.com/rikulo/stomp). @Revalfire implement into stomp_dart implementation for allow connection stomp over sockjs hovewer it looks that today there are some problems. https://github.com/rikulo/stomp/issues/11

dart-js interoperability:
So we decided to try to use javascript version for sock.js and stomp.js and use js-dart interoperability.
We need from dart call some js methods for establishing sock_js connection, sending data over stomp, and then call dart code from js when there is some changes on backend.
Calling js code from dart is running, we establish sock_js connection with server, call succesfully method "sending" in js however there are some hard debug js errors in the sending data to server:

stacktrace:

Instance of 'is'

STACKTRACE:
SockJS.prototype.send@http://localhost:8080/build/sockjs-0.3.4.js:1173
Client.prototype._transmit@http://localhost:8080/build/stomp.js:114
Client.prototype.send@http://localhost:8080/build/stomp.js:276
@http://localhost:8080/build/app.js:33
.l9.K9<@http://localhost:8080/build/main.dart.js:17965
...


our dart code:
    var conn = new JsObject(context['Streaming']);
    conn.callMethod('connect'); //same other
    conn.callMethod('sending');

our js code:
            var Streaming = function() {         
            var stompClient = null;             
              this.connect = function() {
            var socket = new SockJS('/hello');
            stompClient = Stomp.over(socket);           
            stompClient.connect('', '', function(frame) {
                console.log('Connected: ' + frame);
                stompClient.subscribe('/topic/greetings', function(greeting){
                    console.log("notification");
                });});};
           
           
            this.sending = function(){      
                console.log("I'm in sending");    
                stompClient.send("/app/hello", {}, JSON.stringify({ 'name': "hi dart" }));
            }; };

Can you help us how to tackle that problem? Thanks a lot.

Vyacheslav Egorov

unread,
Feb 17, 2014, 9:43:34 AM2/17/14
to w...@dartlang.org
Should not you actually wait until the socket is connected? The API is asynchronous so you can't call send immediately after connect, you should wait until after you got a callback from stompClient.connect.

Lines 1172-1173 is pretty unambiguous about what happens: 

    if (that.readyState === SockJS.CONNECTING)
        throw new Error('INVALID_STATE_ERR'); 



// Vyacheslav Egorov


--
You received this message because you are subscribed to the Google Groups "Dart Web Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web+uns...@dartlang.org.
Visit this group at http://groups.google.com/a/dartlang.org/group/web/.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/web/4cbace52-6cbb-477b-8ae2-b68acff2d827%40dartlang.org.

peter ferko

unread,
Feb 18, 2014, 12:22:37 PM2/18/14
to w...@dartlang.org
Yes, problem was calling function before establishing connection. So now it works(connection, sending, subscribing). Now when there are subscribed some data from server websocket, its' need to send it from javascript to dart, so how to call dart code from javascript ?
btw. Maybe I create for that interoperability new separate post.



Dňa pondelok, 17. februára 2014 15:43:34 UTC+1 Vyacheslav Egorov napísal(-a):

Justin Fagnani

unread,
Feb 18, 2014, 4:37:44 PM2/18/14
to w...@dartlang.org
On Tue, Feb 18, 2014 at 9:22 AM, peter ferko <jsoa...@gmail.com> wrote:
Yes, problem was calling function before establishing connection. So now it works(connection, sending, subscribing). Now when there are subscribed some data from server websocket, its' need to send it from javascript to dart, so how to call dart code from javascript ?
btw. Maybe I create for that interoperability new separate post.

You can just pass Dart functions into JavaScript, and for the most part they are callable. Arguments will be converted according to the normal JS-Dart conversions in dart:js. Named arguments are not supported, of course.

As an example, you can do this:

import 'dart:js' as js;

js.context['dartFunc'] = (message) => print(message);

and in JS:

dartFunc("Hello Dart");

 

Sébastien Deleuze

unread,
Feb 19, 2014, 3:51:37 AM2/19/14
to w...@dartlang.org
Hi,

I am currently working on getting Spring 4 and SockJS Dart client working together. I will post a message here when support will be ok, I think I will have to make some improvements to sockjs-dart-client.
Waiting for my fixes and feedback, one solution could be to use latest Stomp Dart lib (0.7.1 https://pub.dartlang.org/packages/stomp) which is now compatible with Spring Websocket support. See https://jira.springsource.org/browse/SPR-11328 for more details.

When SockJS support will be OK, you just will have to move from pure Websocket to SockJS.

Regards,
Sébastien

peter ferko

unread,
Feb 19, 2014, 9:15:22 AM2/19/14
to w...@dartlang.org
Yes, Thanks Sébastien it would be really great to have that support in sockjs dart.


Dňa streda, 19. februára 2014 9:51:37 UTC+1 Sébastien Deleuze napísal(-a):
Reply all
Reply to author
Forward
0 new messages