Sun. Jan 19th, 2020

Host your Website

Read and learn

HTML5 WebSockets Stock Exchange Simulator: JavaScript & PHP

1 min read

This is a demo for a project I worked on for school (BYU-Idaho). It implements an HTML5 WebSocket server in PHP. The client side is implemented using HTML5 Javascript. Right now the only browsers that run this websocket api properly are Chrome and Safari. While FireFox does support websockets, it seems that it implements an older version of it (Firefox implements version 75, and my PHP server follows the specifications defined in version 76).

The WebSocket specification—developed as part of the HTML5 initiative—introduced the WebSocket JavaScript interface, which defines a full-duplex single socket connection over which messages can be sent between client and server. The WebSocket standard simplifies much of the complexity around bi-directional web communication and connection management.

WebSocket represents the next evolutionary step in web communication compared to Comet and Ajax. However, each technology has its own unique capabilities. Learn how these technologies vary so you can make the right choice. (

Update: I’ve posted the complete source code for this application, along with some extra explanation on how to run the app on your own machine. Download the source code at

23 thoughts on “HTML5 WebSockets Stock Exchange Simulator: JavaScript & PHP

  1. Hallo, i just want to say thank you though for on the server side everything is fine, however when i run index.html on google chrome version 25 it just keeps browsing it brings the h1 header information but is not showing anything else, i am using xampp server on ubuntu 12.10 i don't know what the problem could be, i am able to make socket connections using ordinary php…please advise because my school project requires to use websockets to visualise irregular datasets coming from the server

  2. how can I use the socket?
    I use wamp on windows, I was in command of the windows and typed:
    c: wamp bin php php5.3.5 php-q server.php
    and shows the error:
    Fatal error: Call to undefined function socket_create ()

  3. I got it to work in chrome 19 and firefox 12 using:
    plus I had to make a couple changes that took a few hour to figure out but it works now. The problem was about chrome not liking the server masking the messages being passed back to the browser in chrome 19.
    also look at:
    github . com/lemmingzshadow/php-websocket

  4. A websocket is nothing more and nothing less than a TCP connection to a server through a socket. To make it work in PHP you need to be able to set up plain old PHP sockets, which means you'll need to enable php_sockets.dll. See for more info.

  5. All that Web Sockets is is a TCP connection to your server that you make directly from Javascript. If you ever worked with sockets in PHP (or Java), that is the exact same thing. The only difference with Web Sockets is the handshake process. Once the browser has verified the handshake with the server, the browser will maintain the socket connection, and the API will be available to your app.

  6. Well, once you have some sort of server working (so the connection with the client remains open through the socket), the API is identical to how HTML5's web workers works. So if you want some experience with moving variables around, I'd recommend you play around with Workers. You could just create a separate Javascript class to simulate the server, and access it on your main application through Workers.

  7. I'll test it on my Safari VB, I guess. Not that I don't appreciate the tutorial, man. These websockets tuts are always so complex, though. I've been trying to pay someone 50 bucks to just show me how to pipe variables and strings in and out of a page from a terminal window and a user input, respectively. No takes, though. ^^ It's a little frustrating. XD

  8. and stock.js. ive tried to change localhost to the ip address of the machine, hoping to be able to reach the socket from another machine on the same network. Any ideas?

  9. Awesome work! Very nicely done. I want to implement this on my website, but i have one small problem. How do i get it to work from any other host then "localhost". Ive tried changing $pHost in websocket.server.php and websocket.socket.php, with no success. Id just like to try this from another machine. Any suggestions? Thanks

Comments are closed.

Copyright © All rights reserved. | Newsphere by AF themes.