Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets production ready server in Java?

Tags:

java

websocket

EDIT: removed reference to C# as the only accepted answer is about Java. If someone needs information about websocket server implementation in C#, ask a new question.

Do you know "production ready" framework for creating WebSockets Server in Java? I found one library http://nugget.codeplex.com/ but i did not know how it is stable and fast.

like image 903
Edward83 Avatar asked Nov 25 '10 15:11

Edward83


People also ask

How many WebSockets can a server handle?

By default, a single server can handle 65,536 socket connections just because it's the max number of TCP ports available.

Does spring boot support WebSocket?

Spring Boot includes the spring-WebSocket module, which is compatible with the Java WebSocket API standard (JSR-356). Implementing the WebSocket server-side with Spring Boot is not a very complex task and includes only a couple of steps, which we will walk through one by one.


2 Answers

The accepted answer is 3 years old, with the recent release of JEE7, now every Web Containers that implement servert 3.1 will support websocket via standard API (javax.websocket) package.

The following code show example how to implement websocket using JEE7:

import java.util.logging.Level; import java.util.logging.Logger; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint;  @ServerEndpoint(value = "/chat") public class ChatServer {      private static final Logger LOGGER =              Logger.getLogger(ChatServer.class.getName());      @OnOpen     public void onOpen(Session session) {         LOGGER.log(Level.INFO, "New connection with client: {0}",                  session.getId());     }      @OnMessage     public String onMessage(String message, Session session) {         LOGGER.log(Level.INFO, "New message from Client [{0}]: {1}",                  new Object[] {session.getId(), message});         return "Server received [" + message + "]";     }      @OnClose     public void onClose(Session session) {         LOGGER.log(Level.INFO, "Close connection for client: {0}",                  session.getId());     }      @OnError     public void onError(Throwable exception, Session session) {         LOGGER.log(Level.INFO, "Error for client: {0}", session.getId());     } } 

Example in details here.

Web Container that support Websocket:

  • Tomcat 8
  • WildFly (Previously Jboss AS)
  • Glassfish 4.0
  • and much more
like image 178
Pau Kiat Wee Avatar answered Oct 14 '22 14:10

Pau Kiat Wee


For Java, check out this informative post. Copy-paste from there:

  • Jetty WebSocket Server – Jetty has supported WebSockets since last September. This seems to be a good option.
  • Caucho Resin
  • jWebSocket
  • GlassFish/Grizzly (see a DZone posting on it here)
  • JBoss Netty (see patch here)
  • Webbit

Out of these options, I guess Jetty and Resin are the most mature and stable. However, always good to do your own testing.

like image 37
Neeme Praks Avatar answered Oct 14 '22 14:10

Neeme Praks