简体   繁体   中英

Is websocket a right technology for Chat Application?

I created one small chat App using websocket in that user can join chat-room and can chat with multiple user or in group.

May be at a time maximum 80 to 100 users can send message(this is my requirement)

So my question is that websocket is usefull for me?

Yes it is.

In fact, a chat is the single most common example of web socket application.

http://socket.io/get-started/chat/

I think what you want to ask is: "can a server hold good performance with 100+ websocket connections active"?

The answer is also: definitively yes.

The proof are benchmarks. A single server can handle easily more than 1000 websocket connections without problem.

See:

Websockets can be used to allow each client to get updates from your server without periodically polling the server.

If you want to be realy responsive (ie show the text as people are typing) then Websockets are what you need. However if you are happy to wait 1second to show a message after it has been send you could also use a periodic get request.

However consider the browsers that will be using your app. see: Can I Use Websockets for a compatibly.

To add to what Matthaus Woolard said, not all browsers support websocket ie Opera mini, so to make your application work-proof, you need to design with websocket and long polling ,. To detect websocket availability you can use the $.get or $.ajax method to send a request to the WebSocket endpoint. If the connection is successful, the onopen event will be triggered, and you can use the onmessage event to handle incoming messages. If the connection is unsuccessful, the onerror or onclose event will be triggered. Here is an example:

var socket = new WebSocket("ws://example.com/ws");

socket.onopen = function() {
    console.log("WebSocket connection opened.");
    //run your websocket node.js here
};

socket.onmessage = function(event) {
    console.log("Received message: " + event.data);
    //run your websocket node.js here
};

socket.onerror = function(event) {
    console.log("WebSocket error: " + event);
   //fall back to long polling here
};

socket.onclose = function() {
    console.log("WebSocket connection closed.");
   //fall back to long polling he
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM