Giter VIP home page Giter VIP logo

pguide's Introduction

pguide

Welcome.

Realtime messaging with MySQL, and bit of php

This will have least requirements.

Database structure

Note: SERIAL is: BIGINT UNSIGNED UNIQUE AUTO_INCREMENT

Table channel has following fields:

  • id SERIAL, not primary
  • name VARCHAR(255)

Optional fields:

  • logo_blob BLOB
  • logo_url VARCHAR(2048)

Table messages has following fields:

  • id SERIAL, not primary
  • uid BIGINT, attribute: UNSIGNED
  • message TEXT, or VARCHAR
  • channel_id BIGINT UNIQUE, attribute: UNSIGNED

Table last_message
This table does not spam with INSERT, it's done only once,
Meaning: every time new message is sent, UPDATE must be called immediately.

Has following fields:

  • last_id BIGINT, attribute: UNSIGNED
  • channel_id BIGINT UNIQUE, attribute: UNSIGNED
  • uid BIGINT, attribute: UNSIGNED

The channel_id will crash INSERT if duplicate id is found. Thus UPDATE must be called instead.



Assuming that php will be used, you would want to use usleep(256e3) for the MINIMUM delay between each fetchall.
However if you are sure your server is capable of higher stress, use usleep(128e3) instead.
For free hosting, about usleep(500e3) is recommended.


This is how you do realtime fetch:

// dummy functions, let's assume you wrote actual code
function fetchAll($last_msg){
  $last_id = $last_msg->last_id;
  $q = "SELECT * from messages where id>$last_id";
  //...
}
function get_last_message($channel_id){
  $q = "SELECT * from last_message where channel_id='$channel_id'";
  //...
}
function sendmessage(){
  // or use INSERT, if not inserted!
  $q = "UPDATE last_message set ...";
  // you know what to do here:
  $q2 = "INSERT into messages set ...";
  // ...
}

$channel_id = 1;
$maxticks = 32;
$last_msg = get_last_message($channel_id);
// last_msg contains last_id which is later used by fetchAll to fecth messages AFTER that id
while ($maxticks--) {
  $curr_msg = get_last_message($channel_id);
  if ($last_msg->last_id != $curr_msg->last_id) {
    $last_msg = $curr_msg;
    break;
  }
  usleep(500e3);
}
echo fetchAll($last_msg);

pguide's People

Contributors

slowsient avatar

Watchers

 avatar

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.