Giter Site home page Giter Site logo

arkanis / simple-chat Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 44 KB

A simple chat in ~20 lines of PHP and ~50 lines of JavaScript. Doesn't need a database.

Home Page: http://arkanis.de/projects/simple-chat/

License: MIT License

PHP 90.02% Ruby 9.98%
chat javascript no-dependencies php simple small-scale

simple-chat's Introduction

Simple Chat

In ~20 lines of PHP and ~50 lines of plain JavaScript. No dependencies and no database needed.

Meant for small chats. Not good for chats with a lot of users since every user looks for new messages every 2 seconds.

Demo

Requirements

  • A webspace that supports PHP
  • A directory in that webspace that is writable by the webserver. You might have to mark the directory writable in your admin interface.

Installation

  • Copy index.php into the directory
  • Done

The chat will create a messages.json file the first time someone writes a message. That's why the directory needs to be writable. Alternatively you can leave the directory read-only and create an empty writable messages.json.

Optional: Enable the chatlog

By default the chat only remembers the last 50 messages. That way the chat cleans itself up automatically.

If you want a full history of all posted messages have to edit index.php. Change the line $enable_chatlog = false; to $enable_chatlog = true;.

With that every message gets appended to chatlog.txt. This is not shown in the chat, it's just a log for yourself. It will grow with every posted message, so remember to clean it up if it grows to large.

Feedback and comments

If you have questions about this chat or want to share some thoughts or ideas, feel free to open an issue or post a comment on the blog post of the project. You can also send me a mail if you prefer that (see here).

How the chat works

The idea is simple:

  • Append new messages to a file on the server, but only keep the last few messages.
  • Every client requests this file every two seconds and displays all new messages inside it (this approach is called "polling").

These polling requests are relatively cheap thanks to HTTP caching. But with a lot of users in the chat this adds up quickly and can get quite inefficient.

More complex chats can do a lot better but also require fancy stuff like WebSockets, way more code and usually a more complex setup. For simple usecases with just a few users a simple chat gets the job done easily.

This simple design leads to a chat that doesn't need a database nor a large server or infrastructure. Just a small bunch of lines you can drop into your project and modify or extend as needed. It's so simple that it shouldn't be a problem to extend or adopt the code for your own purpose (e.g. multiple chat rooms, usage as message API, fancy styling, etc.).

If you want to know more take a look at the detailed explanation of the design and implementation of the first version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.