Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on an application that requires a lot of user interaction. Its kind of a discussion form where people can comment. Currently we are using web services and every time user post a comment or a reply to a comment we are calling a webservice and it is communicating with database and doing the rest of the things. I found this process rather slow. So i read in few places that web sockets may be solution to my problem where I can directly use available apis to commiunicate with database and make my application faster. I searched a lot, in some examples available online they were using servlets too and in some they were not. It was very confusing. I just want to use html5 websockets. the UI code is a page which will send some text to backend. The JS code is:

<script>
var connection;



function connect() {
    console.log("connection");
    connection = new WebSocket("not sure what exactly to use here");
    // Log errors 
connection.onerror = function (error) {
  console.log('WebSocket Error ');
  console.log(error);

};

// Log messages from the server 
connection.onmessage = function (e) {
  console.log('Server: ' + e.data); 
  alert("Server said: " + e.data);
};

connection.onopen = function (e) {
console.log("Connection open...");
}

connection.onclose = function (e) {
console.log("Connection closed...");
}
}


function sayHello() {
    connection.send(document.getElementById("msg").value);
}

function close() {
    console.log("Closing...");
    connection.close();
}
</script>

while creating new WebSocket object what path exactly i need to mention. Should I use servlets or not. Please give ideas about the backend java code. Thanks in advance

share|improve this question
    
I'm not sure that websockets will make the posting of new comments any quicker.As I understand it, it makes User Interfaces that that generally do a lot of polling more responsive ( so other users would see the posts quicker and without refreshing their UI ), but I don't see that the post will be ant quicker than using a web service. Before going down this route, you need to identify which is the slow bit - you may be solving the wrong problem here –  DaveH Jun 24 '13 at 7:24

1 Answer 1

up vote 1 down vote accepted

Servlet has not such support. You should use WebSocket of Java EE 7. Your code should be like this

@ServerEndpoint("/echo")
public class EchoEndpoint {
   @OnMessage
   public void onMessage(Session session, String msg) {
      try {
         session.getBasicRemote().sendText(msg);
         //Save message here into database 
      } catch (IOException e) { ... }
   }
}

For Details, see here: http://docs.oracle.com/javaee/7/tutorial/doc/websocket004.htm

share|improve this answer
    
I am new at all these, I am not sure what serverendpoint path I should pass from the javascript while sending a message if my java program is in some package –  kavinder Jun 24 '13 at 6:17
    
Java EE7 websocket directly works with JavaScript.i.e the browser where WebSocket is support. For that you need to go through the tutorial and run the example given at tutorial. –  Masud Jun 24 '13 at 6:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.