1

I want to create a basic chat application using web socket in JavaScript. I searched a lot about it on various search engines but I didn't found anything that could help me.

I am using Wamp server and don't have NODE.js installed.

  1. Can I create an application on Wamp?
  2. How to write a server code for Web Sockets?

Note: I am aware of Client code for web socket but don't from where to start with server code? The code I need can be in PHP or JavaScript. Below is a basic client code written by Me.

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest()
{
  if ("WebSocket" in window)
{
  alert("WebSocket is supported by your Browser!");
 // Let us open a web socket
 var ws = new WebSocket("ws://localhost:9998/echo");
 ws.onopen = function()
 {
    // Web Socket is connected, send data using send()
    ws.send("Message to send");
    alert("Message is sent...");
 };
 ws.onmessage = function (evt) 
 { 
    var received_msg = evt.data;
    alert("Message is received...");
 };
 ws.onclose = function()
 { 
    // websocket is closed.
    alert("Connection is closed..."); 
 };
}
else
{
   // The browser doesn't support WebSocket
    alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<div id="sse">
   <a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>
Wasimakram Mulla
  • 511
  • 7
  • 24

2 Answers2

0

sailsjs is a great way to get started building a real-time web application using WebSockets. Sails uses express and socket.io to enable WebSockets functionality.

See this SailsCast to get started: Building a Sails Application: Ep21 - Integrating socket.io and Sails With Custom Controller Actions Using Real Time Model Events.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
  • Even though this is tagged `node.js`, I just saw that you're using Wamp. Besides that being a horrifying requirement in general, sails.js will not run on php. – Travis Webb Apr 17 '15 at 10:51
  • Hello I would advise against using the above tutorial... it was written for an old version of Sails, and the commands described in the video don't work on the current Sails 0.11.... might be setting someone up for a frustrating experience :-) If I have time will try to write a socket io tutorial for sails, but in summary it is pretty straightforward, check this post http://stackoverflow.com/questions/22454809/emitting-socket-io-event-from-sails-js-and-handling-it-on-server-using-sails-js – Stenio Ferreira Apr 17 '15 at 15:53
0

If you are looking into a basic chat application you do not need web sockets, an AJAX polling will suffice.

The problem with PHP web sockets is that to use them you will need to create your own PHP server that will open a socket and listen on it. To do so you need to be able to run PHP scripts as CLI - you will need to have shell access on your server, which is not available on most shared hosting providers.

Community
  • 1
  • 1
Tomasz Cz.
  • 315
  • 5
  • 22