Jetty websocket and Dropwizard.

1,320 views
Skip to first unread message

Ninad tare

unread,
Nov 11, 2014, 8:10:13 AM11/11/14
to dropwiz...@googlegroups.com
hi!
I have a service running in version 0.7.1.
I am searching for a way to implement websocket in my service in my search i found this thread . In it Nick Telford is pointing to using Jetty websocket and even Olve Hansen had managed to use it.
Any one could point me out any example of how to do it?

The actual requirement i landed up thinking about web socket is that: the service handles analytics queries from a dashboard, the back-end used is MongoDB for this, now we want to implement a viewer something like a speedometer and for this my manager is emphasizing on using websocket so the use case is like a client request will com from dashboard and its session will be maintained and whenever data comes in MongoDB(we have written a resource to handle insertion of records to mongodb) it will identify session on which data needs to be send and send data to that session.
I am very new to websockets if anything i am missing please point me out.

Doug Patriarche

unread,
Nov 11, 2014, 5:15:41 PM11/11/14
to dropwiz...@googlegroups.com
Hi Ninad,

Another option is to use Dropwizard 0.8.0 (currently available as a release candidate 0.8.0-rc1), which includes Jetty 9.2.4, which supports the standard websocket API. Using the standard API will give you more portability going forward.

- Doug

Pushkaraj Chitre

unread,
Nov 11, 2014, 5:20:29 PM11/11/14
to dropwiz...@googlegroups.com
The way I got it to work was by adding jetty's websocket servlet to jersey environment in the run() method of service. You can configure the urlmapping on that object too. It's been working fine for me. But as Doug suggested, I'd just use 0.8 .0 if you can. Would be much easier.

Ninad tare

unread,
Nov 11, 2014, 11:44:58 PM11/11/14
to dropwiz...@googlegroups.com
Hi! PushKaraj Chitre, i have managed to create a websocket by adding jetty websocket dependency in POM and the same way as Olve Hansen had managed it. But still am not sure about the session as their is nothing to identify them and how will i maintain them.
Ya will try v0.8.0 once officially released as the service is production one, i can only upgrade to stable release.

Olve Hansen

unread,
Nov 13, 2014, 11:13:16 AM11/13/14
to dropwiz...@googlegroups.com
The session is maintained by the container. But you can put them in e.g. several maps based on what you want to broadcast. Say you want to broadcast a notification you iterate over all sessions and send the msg via the RemoteEndpoint
or in DW 0.8
(async or basic).

The session can be closed from either side at any time, so error handling is a must.

Ninad tare

unread,
Nov 13, 2014, 11:49:11 PM11/13/14
to dropwiz...@googlegroups.com
hi! olve Hansen!
I have added it now in my running service the websocket implementation. But for managing session i need id a unique id and org.eclipse.jetty.websocket.api.Session doesnt provide a way to get unique identity for that session instance as provided by javax.websocket.Session. An ya we are handling session closing events.

As using jetty we register our end point class like this 

import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;


public class RESTWebSocketServlet extends WebSocketServlet {

private static final long serialVersionUID = 1L;

public void configure(WebSocketServletFactory factory) {
factory.register(WebSocketEndpoint.class);
}
}

do we have to register endpoint class create using javax.websocket api? yes how can we do that?


Olve Hansen

unread,
Nov 14, 2014, 4:41:08 AM11/14/14
to dropwiz...@googlegroups.com

This is a bit off topic for Dropwizard now :-)

Why do you need to get the unique id of a session? That is like meddling with the JSESSIONID in stateful servlet containers. To add your own identifiers, add them as parameters to the upgrade request, so that you get hold of them on the jetty/framework callback to org.eclipse.jetty.websocket.servlet.WebSocketCreator#createWebSocket

I see that I did not describe the WebSocketCreator in the other thread, so it would be something like this:

public class MyWebSocketServlet extends WebSocketServlet implements WebSocketCreator {
 @Override
    public void configure(WebSocketServletFactory factory) {
        factory.register(MyWebSocketEndpoint.class);
        factory.setCreator(this);
    }
    @Override
    public Object createWebSocket(UpgradeRequest req, UpgradeResponse resp) {
        MyWebSocketEndpoint ep = new MyWebSocketEndpoint(req.getgetParameterMap());
        registerMyEndpoint(ep);
        return ep;
    }
}

Put the endpoint you create (and return) from this method in a storage of liking (e.g. a map with some id and the endpoint as value) and you have your session-lookup utility if you want to do server-side initiated push events.

You have to register the endpoint class yes - I described that in the other thread:
https://groups.google.com/d/msg/dropwizard-user/doNCx_35urk/5PIvd8_NHIcJ


HTH


--
You received this message because you are subscribed to a topic in the Google Groups "dropwizard-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dropwizard-user/GTQrzYiVA5Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Olve Sæther Hansen
Senior Developer | Vimond Media Solutions
Eol...@vimond.com

Ninad tare

unread,
Nov 14, 2014, 6:58:26 AM11/14/14
to dropwiz...@googlegroups.com
wohoo! that was really helpfull thanks for that tips to add parameters in upgrade request.
Ya it was off topic as dropwizard sorry for that!
Thank you :-)  as on the first step off adding websocket in dropwizard was possible for me by your answer https://groups.google.com/forum/#!msg/dropwizard-user/doNCx_35urk/5PIvd8_NHIcJ
Thank you,
Ninad A. Tare

Olve Hansen

unread,
Nov 14, 2014, 10:03:16 AM11/14/14
to dropwiz...@googlegroups.com
You're welcome! :)

fross

unread,
Feb 7, 2015, 10:25:55 PM2/7/15
to dropwiz...@googlegroups.com
"I'd just use 0.8 .0 if you can. Would be much easier."

Newbie question here: how is 0.8 supposed to make things easier? I just wrote a small server app to test websockets using 0.8. The app is setup exactly like you describe it: a Jetty's websocket servlet added in the application run() method. Am I missing something?

Olve Hansen

unread,
Feb 8, 2015, 4:10:32 PM2/8/15
to dropwiz...@googlegroups.com
I think that what was he meant by the 0.8.0 comment was that it runs a jetty version supporting JSR 356

 O. 
--
You received this message because you are subscribed to a topic in the Google Groups "dropwizard-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dropwizard-user/GTQrzYiVA5Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Sendt fra Gmail for mobil

fross

unread,
Feb 9, 2015, 11:53:14 AM2/9/15
to dropwiz...@googlegroups.com
That's also what I'm thinking but I wasn't 100% sure.
To unsubscribe from this group and all its topics, send an email to dropwizard-user+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages