Comet and WebSocket support

202 views
Skip to first unread message

Ivan Porto Carrero

unread,
Jan 16, 2011, 5:06:01 PM1/16/11
to scalat...@googlegroups.com
Hi guys

Today I've pushed comet and websocket support for scalatra.
For now we're using a java based implementation of http://socket.io which can be found at: http://code.google.com/p/socketio-java 

Because socket.io makes the connection with the browser look like a socket it's probably best to have only one servlet that extends SocketIOSupport and use the onMessage handler to dispatch process messages.
And for the flash policy server to work you need to start jetty with administrative privileges because it uses port 843


## WebSocket and Comet support through Socket.IO

Scalatra provides optional support for websockets and comet through [socket.io](http://socket.io). We depend on [the socketio-java project](http://code.google.com/p/socketio-java) to provide this support.

1. Depend on the scalatra-socketio.jar. In your SBT build:

       val scalatraSocketIO = "org.scalatra" % "scalatra-socketio" % scalatraVersion

2. SocketIO mimics a socket connection so it's easiest if you just create a socketio servlet at /socket.io/*

       import org.scalatra.ScalatraServlet
       import org.scalatra.socketio.SocketIOSupport

       class MySocketIOServlet extends ScalatraServlet with SocketIOSupport {
         // ...
       }

3. Setup the callbacks

       socketio { socket =>

         socket.onConnect { connection =>
           // Do stuff on connection
         }

         socket.onMessage { (connection, frameType, message) =>
           // Receive a message
           // use `connection.send("string")` to send a message
           // use `connection.broadcast("to send")` to send a message to all connected clients except the current one
           // use `connection.disconnect` to disconnect the client.
         }

         socket.onDisconnect { (connection, reason, message) =>
           // Do stuff on disconnection
         }
       }

4. Add the necessary entries to web.xml

       <servlet>
         <servlet-name>SocketIOServlet</servlet-name>
         <servlet-class>com.example.SocketIOServlet</servlet-class>
         <init-param>
           <param-name>flashPolicyServerHost</param-name>
           <param-value>localhost</param-value>
         </init-param>
         <init-param>
           <param-name>flashPolicyServerPort</param-name>
           <param-value>843</param-value>
         </init-param>
         <init-param>
           <param-name>flashPolicyDomain</param-name>
           <param-value>localhost</param-value>
         </init-param>
         <init-param>
           <param-name>flashPolicyPorts</param-name>
           <param-value>8080</param-value>
         </init-param>
      </servlet>  

              
When you want to use websockets with jetty the sbt build tool gets in the way and that makes it look like the websocket stuff isn't working. If you deploy the war to a jetty distribution everything should work as expected.


The web.xml config:

The SocketIOSupport servlet

The chat client:


To run the example:
sbt 'project scalatra-example' update package
cp example/target/scala_2.8.1/scalatra-example_2.8.1-2.0.0-SNAPSHOT.war /path/to/jetty/webapps/ROOT.war
cd /path/to/jetty; sudo java -jar start.jar; cd -

It's best that you open different browsers altogether ie:


--

Met vriendelijke groeten - Best regards - Salutations

Ivan Porto Carrero

Stuart Roebuck

unread,
Jan 28, 2011, 12:09:30 PM1/28/11
to scalatra-user
Ivan,

I'm trying to get socket.io up and running but I'm getting an
IllegalAccessError on the socketio SocketIOSessionManager class:

java.lang.IllegalAccessError: tried to access class
com.glines.socketio.server.SocketIOSessionManager from class
org.scalatra.socketio.SocketIOSupport$class
at org.scalatra.socketio.SocketIOSupport
$class.initialize(SocketIOSupport.scala:133)

any thoughts on where I may be going wrong?

I'm using: socketio-java-20101204.jar downloaded from:
http://code.google.com/p/socketio-java/downloads/detail?name=socketio-java-20101204.jar

Best,

Stuart.
> The web.xml config:https://github.com/scalatra/scalatra/blob/master/example/src/main/web...
>
> The SocketIOSupport servlethttps://github.com/scalatra/scalatra/blob/master/example/src/main/sca...
>
> The chat client:https://github.com/scalatra/scalatra/blob/master/example/src/main/web...
>
> To run the example:
> sbt 'project scalatra-example' update package
> cp example/target/scala_2.8.1/scalatra-example_2.8.1-2.0.0-SNAPSHOT.war
> /path/to/jetty/webapps/ROOT.war
> cd /path/to/jetty; sudo java -jar start.jar; cd -
>
> It's best that you open different browsers altogether ie:
> chrome:http://localhost:8080/chat
> firefox:http://localhost:8080/chat
> safari:http://localhost:8080/chat
>
> --
>
> *Met vriendelijke groeten - Best regards - Salutations*
>
> *Ivan Porto Carrero*

Ivan Porto Carrero

unread,
Jan 28, 2011, 12:22:16 PM1/28/11
to scalat...@googlegroups.com
Hi,

I made a few changes to the socketio library for it to work with scalatra.
So I think you should use the one that's in the scalatra source tree.


hope this helps
--

Met vriendelijke groeten - Best regards - Salutations

Ivan Porto Carrero

Stuart Roebuck

unread,
Jan 28, 2011, 12:48:26 PM1/28/11
to scalatra-user
Many thanks.

That helps a lot :)

Stuart

On Jan 28, 5:22 pm, Ivan Porto Carrero <i...@mojolly.com> wrote:
> Hi,
>
> I made a few changes to the socketio library for it to work with scalatra.
> So I think you should use the one that's in the scalatra source tree.
>
> https://github.com/scalatra/scalatra/tree/master/socketio/lib
>
> hope this helps
>
> On Fri, Jan 28, 2011 at 6:09 PM, Stuart Roebuck <stuart.roeb...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > Ivan,
>
> > I'm trying to get socket.io up and running but I'm getting an
> > IllegalAccessError on the socketio SocketIOSessionManager class:
>
> >  java.lang.IllegalAccessError: tried to access class
> > com.glines.socketio.server.SocketIOSessionManager from class
> > org.scalatra.socketio.SocketIOSupport$class
> >        at org.scalatra.socketio.SocketIOSupport
> > $class.initialize(SocketIOSupport.scala:133)
>
> > any thoughts on where I may be going wrong?
>
> > I'm using: socketio-java-20101204.jar  downloaded from:
>
> >http://code.google.com/p/socketio-java/downloads/detail?name=socketio...
Reply all
Reply to author
Forward
0 new messages