') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); GitHub - ludis/codeigniter-websocket: Codeigniter WebSocket Built for real-time application. Inspired by https://github.com/romainrg/ratchet_client · GitHub
Skip to content

Repository files navigation

Latest Version on PackagistTotal Downloads

CodeIgniter WebSocket Library

CodeIgniter WebSocket library. It allows you to make powerfull realtime applications by using Ratchet (Socketo.me & ratchet_client) Websocket technology.

WebSocket Library for Codeigniter 4.x https://github.com/takielias/codeigniter4-websocket

If you Face any problem you may check CodeIgniter WebSocket Example https://github.com/takielias/codeigniter-websocket-example

📚 Dependencies

  • PHP 5.6+
  • CodeIgniter Framework (3.1.* recommanded)
  • Composer
  • PHP sockets extension enabled

🔰 Installation

➡️ Step 1 : Library installation by Composer

Just by running following command in the folder of your project :

composer require takielias/codeigniter-websocket

Don't forget to include your autoload to CI config file :

$config['composer_autoload'] = FCPATH.'vendor/autoload.php';

➡️ Step 2 : One command Setup

If you want Single command installation just Execute the Command in the Project directory

N.B: It will make 2 new controllers Welcome.php and User.php

php vendor/takielias/codeigniter-websocket/install.php --app_path=application

Here app_path defines your default Codeigniter Application directory Name

one click installation

WOW You made it !!! ✔️

Open two pages of your project on following url with different IDs :

http://localhost/your project directory/index.php/user/index/1

http://localhost/your project directory/index.php/user/index/2

❗ In this example, recipient_id is defined by user_id, as you can see, it's the auth callback who defines recipient ids.

If you have something like that, everything is ok for you:

user_1

user_2

You can try typing and sending something in each page (see cmd for more logs).

cmd

➡️ Run the Websocket server Manually

If you want to enable debug mode type the command bellow in you'r project folder :

php index.php welcome index

If you see the message the message bellow, you are done (don't close your cmd) !

First_launch.png

➡️ Test the App

Broadcast messages with your php App 💥 !

If you want to broadcast message with php script or something else you can use library like textalk/websocket(who is included in my composer.json as required library)

Note : The first message is mandatory and always here to perform authentication

$client = newClient('ws://0.0.0.0:8282');
$client->send(json_encode(array('user_id' => 1, 'message' => null)));
$client->send(json_encode(array('user_id' => 1, 'message' => 'Super cool message to myself!')));

Authentication & callbacks ♻️

The library allow you to define some callbacks, here's an example :

class Welcome extends CI_Controller
{
publicfunctionindex()
{
// Load package path$this->load->add_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
$this->load->library('Codeigniter_websocket');
$this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
// Run server$this->codeigniter_websocket->set_callback('auth', array($this, '_auth'));
$this->codeigniter_websocket->set_callback('event', array($this, '_event'));
$this->codeigniter_websocket->run();
}
publicfunction_auth($datas = null)
{
// Here you can verify everything you want to perform user login.// However, method must return integer (client ID) if auth succedeed and false if not.return (!empty($datas->user_id)) ? $datas->user_id : false;
}
publicfunction_event($datas = null)
{
// Here you can do everyting you want, each time message is receivedecho'Hey ! I\'m an EVENT callback'.PHP_EOL;
}
}
  • Auth type callback is called at first message posted from client.
  • Event type callback is called on every message posted.

Bugs 🐛 or feature 💪

Be free to open an issue or send pull request

Support on Beerpay

Hey dude! Help me out for a couple of 🍻!

BeerpayBeerpay

About

Codeigniter WebSocket Built for real-time application. Inspired by https://github.com/romainrg/ratchet_client

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages