PHP Classes

PHP MySQL WebSocket Chat: Websocket chat that stores messages in MySQL

Recommend this page to a friend!
  Info   View files Documentation   Videos Videos   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog (1)    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 65%Total: 1,680 This week: 2All time: 2,344 This week: 40Up
Version License PHP version Categories
mysql-websocket-chat 1.7MIT/X Consortium ...7HTTP, PHP 5, Databases, Chat
Description 

Author

This package can Websocket chat that stores messages in MySQL using PDO.

It takes HTTP Websocket connections to handle chat messages exchanged between users in a chat room.

It also supports exchanging messages privately between users.

Innovation Award
PHP Programming Innovation award nominee
September 2016
Number 2


Prize: One big elePHPant Plush Mascott
Websockets are great for implementing interactive Web applications that require bidirectional communication between the browser and the server, like for instance chat systems.

This package implements a Websockets based chat system that can store messages in databases, so they can be accessed later if the users were not able to receive them when the messages were sent.

Manuel Lemos
Picture of Johnny Mast
  Performance   Level  
Name: Johnny Mast <contact>
Classes: 10 packages by
Country: The Netherlands The Netherlands
Age: 41
All time rank: 121834 in The Netherlands The Netherlands
Week rank: 45 Up1 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 5x

Recommendations

como configurar un web socket en mi servidor local
como configurar un web socket del lado de servidor y del cliente

php mysql websocket chat
I need to create realtime user to user chat

What is the best PHP chat room class?
I need a simple chat package

Documentation

Code Triagers Badge ESLint StyleLint Phpcs

MYSQL WEBSOCKET CHAT

Welcome to this Hackaton project I created for user hrushi on phpclasses.org. The idea was to create a web socket chat application that could be logging to a database. So here is what you need to get this up and running. *Please note* the minimum required PHP version is 7.0- this is not because it wanted this but it is because of the dependencies this project has.

Step 1: install composer

First thing you is installing composer on to your system. You can get composer here. Don't worry it might seem intimidating but it is not.

Step 2: Install the project

Using composer

Installing the project using composer is hands down the easiest way to get started. This method will download the project from GitHub and automatically install its dependencies for you. Presuming you installed composer (step 1) execute the following commands on the command-line.

$ composer create-project johnnymast/mysql_websocket_chat chat
$ cd chat

<em>In the above example I am using a mac so my prompt will display different then you if you are on windows.</em>

Downloaded from phpclasses.org

If you download this package in a zip file from phpclasses.org you will have to extract the zip package to a location of your liking. Then change directory into that directory and execute the following command on your prompt.

$ composer install

<em>In the above example I am using a mac so my prompt will display different then you if you are on windows.</em>

Step 3: Configure the server

Websocket configuration

This project can be split into two different components. The WebSocket server is the <code>server.php</code> in the root directory. The second part is the frontend part located in <code>public/index.php</code>. For the WebSocket server, there are two configuration options that you can configure in <code>includes/config.php</code>.

WEBSOCKET_SERVER_IP

This flag allows you to configure the WebSocket server's IP-address. By default the value <code>127.0.0.1</code> has been set.

WEBSOCKET_SERVER_PORT

This will configure what port the WebSocket server will listen on. The default value has been set to <code>8080</code>. You can change this value if it clashes with other services running on your machine.

Database configuration

This server can run either with or without a database. By default i have disabled the use of a database server (<code>ENABLE_DATABASE</code>) but you can enable it by switching the <code>ENABLE_DATABASE</code> to <code>true</code> in the includes/config.php file.

| Flag | Description | | --- | --- | | DATABASE_HOST | The database username goes in here. By default this has been set to <code>root</code>. | | DATABASE_PORT | The database port goes in here. By default this has been set to <code>3306</code>. | | DATABASE_USERNAME | The database username goes in here. By default this has been set to <code>root</code>.| | DATABASE_PASSWORD | Enter the password to access the database there. By default this has been set to <code>root</code>.| | DATABASE_DB | Enter the name of the database here. By default this has been set to <code>socket_chat</code>.| | ENABLE_DATABASE | This flag will turn using the database on or off by setting its value to <code>true</code> or <code>false</code>.|

*Please note* if you enable the database make sure you update the credentials as well (see table above). Also, if you enable the database make sure you have imported database.sql into your database.

Step 4: Fire up the WebSocket server

Change direction into the chat directory and fire up the server.

$ cd chat
$ php ./server.php

When you see no output and the command seems to hang that's when you know its running.

Step 5: Point a web service to the public directory

In the chat directory, you will find index.php. This file will be the client for your chat application. Make sure you set any web service its document root to the <code>public/</code> folder. Alternatively, if you don't have access to a webserver you can also try using PHP's build-in webserver.

$ cd public
$ php -S 127.0.0.1:8000

<emn>This will start an webserver on port 8000</em>

Step 6: Chat away!

Now open up 2 chat tabs and point them to localhost (or maybe a virtual host you configured) and chat away with your self.

Functionality

Private chats

If you want to test private chats you can single click any user in the user list on the right of the screen. Then type your message in the message bar, this will send a private message only to that user.

Changes

If you wish to know what has changed in this version of Mysql WebSocket Chat you can always checkout the changelog here.

Author

This package is created and maintained by Johnny Mast. For feature requests or suggestions you could consider sending me an e-mail.

Enjoy

Oh and if you've come down this far, you might as well follow me on twitter.

License

MIT License

Copyright (c) 2021 Johnny Mast

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


  Files folder image Files  
File Role Description
Files folder image.github (1 file, 2 directories)
Files folder imageincludes (1 file, 1 directory)
Files folder imagepublic (1 file, 2 directories)
Accessible without login Plain text file .stylelintrc.json Data Auxiliary data
Accessible without login Plain text file CHANELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file database.sql Data Auxiliary data
Accessible without login Plain text file eslintrc.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file package-lock.json Data Auxiliary data
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file server.php Appl. Application script

  Files folder image Files  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (2 files)
Files folder imageworkflows (3 files)
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Accessible without login Plain text file ISSUE_TEMPLATE.md Data Auxiliary data
  Accessible without login Plain text file REQUEST_A_FEATURE.md Data Auxiliary data

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ESLint.yaml Data Auxiliary data
  Accessible without login Plain text file Phpcs.yaml Data Auxiliary data
  Accessible without login Plain text file StyleLint.yaml Data Auxiliary data

  Files folder image Files  /  includes  
File Role Description
Files folder imageclasses (2 files)
  Accessible without login Plain text file config.php Conf. Configuration script

  Files folder image Files  /  includes  /  classes  
File Role Description
  Plain text file Chat.php Class Class source
  Plain text file Database.php Class Class source

  Files folder image Files  /  public  
File Role Description
Files folder imagecss (1 file)
Files folder imagejs (3 files)
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files  /  public  /  css  
File Role Description
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files  /  public  /  js  
File Role Description
  Accessible without login Plain text file dom.js Data Auxiliary data
  Accessible without login Plain text file interface.js Data Auxiliary data
  Accessible without login Plain text file websockets.js Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:1,680
This week:2
All time:2,344
This week:40Up
 User Ratings  
 
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:65%StarStarStarStar
Rank:648