eventbus bridge Invalid state error

122 views
Skip to first unread message

Siva Phani Kumar

unread,
Sep 7, 2015, 12:33:01 PM9/7/15
to vert.x
Do i need to send events form client side when i open the event bus

i mean 

var eb = new vertx.EventBus(localhost:8080/eventbus)
eb.onopen = function() {
    eb.send();
}

or 

var eb = new vertx.EventBus(localhost:8080/eventbus)

    eb.send();


here the problem is when i try to open eb from external url like

var eb = new vertx.EventBus(https://external_2.com/eventbus)
eb.send()

Note: html code was in external 1 server and sockjs bridge was hosted on external_2 server. Both the servers are different.

Some times i am getting Invalid state error.

when i debug i got found eb try to send with out eventbus opening happen.

what is the right place to sent event. every time onopen..??

if that is the case if i want to send 2 or more than events in one page, how can i send them.

every time do i need to  create eb object and try to open on each time..?? Is it correct way ? or there any better solution to achieve this...???

Thanks in Advance.




Paulo Lopes

unread,
Sep 7, 2015, 2:25:43 PM9/7/15
to vert.x


On Monday, September 7, 2015 at 6:33:01 PM UTC+2, Siva Phani Kumar wrote:
Do i need to send events form client side when i open the event bus

No you don't.
 

i mean 

var eb = new vertx.EventBus(localhost:8080/eventbus)
eb.onopen = function() {
    eb.send();
}

or 

var eb = new vertx.EventBus(localhost:8080/eventbus)

    eb.send();


here the problem is when i try to open eb from external url like

var eb = new vertx.EventBus(https://external_2.com/eventbus)
eb.send()

Note: html code was in external 1 server and sockjs bridge was hosted on external_2 server. Both the servers are different.

Some times i am getting Invalid state error.

The issue is that you assume that code is executed synchronously when it's not. The constructor call async methods from SockJS and when the runtime executes the eb.send() the callback from SockJS is not complete yet (your invalid state). You can know when it is complete using the callback:

eb.onopen = function() {
  // at this moment you're sure the full SockJS handshake is complete and a connection is open to yout sockJS bridge so you can safely send messages!
}


when i debug i got found eb try to send with out eventbus opening happen.

what is the right place to sent event. every time onopen..??

if that is the case if i want to send 2 or more than events in one page, how can i send them.

once the eb is open you can call send as many times as you want you can even loop for(;;) eb.send('DoS', {}); but that is not a good idea ;)
 

every time do i need to  create eb object and try to open on each time..?? Is it correct way ? or there any better solution to achieve this...???

No, just 1 eb object per window object, either make it a singleton or a property of the global context.
 

Thanks in Advance.





Cheers,
Paulo
Reply all
Reply to author
Forward
0 new messages