PHP code example of mzohaibnaz / neosocket

1. Go to this page and download the library: Download mzohaibnaz/neosocket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mzohaibnaz / neosocket example snippets


// Using neoSocket namespace
use NeoSocket\SocketManager;
// Initializing neoSocket with SocketManager
$ns = new  SocketManager();

$ns->setup(function($socket){
	// log on console socket is running
	print("\n\n Socket is Running \n\n");
});

$ns->create("localhost", 1414)->setup(function($socket){
	// log on console socket is running
	print("\n\n Socket is Running  on localhost with port 1414 \n\n");
});

$ns->setup(function($socket){
	// event setup callback take 2 parameter
	// first socket reference. second contain data from the client for that event
	function callback_fnc($socket, $data){
		// do something awesome here
	};
	
	$socket->on("test", callback_fnc);
});

$socket->on("test", function($socket, $data){
	// do something awesome here
});

	// default event whenever there is new connection
	$socket->on("connection", function($socket, $uid){
	// log on console about new user with unique id
	// $uid is a unique id for each user in socket
	print("\n new user is here with id: {$uid}");
	// tell other users that new user is here with event `newuser`
	// that will send data to javascript client of neoSocket with event `newuser`
	// for chat room example tell all other users that,
	// there is new user
	$socket->event("newUser")->send("new user with id! : ".$uid);
});

$socket->on("disconnected", function($socket, $uid){
	echo  "\n user disconnected : {$uid} \n";
	// for chat room example tell other user that user is disconnected
	$socket->event("ondisconnect")->send("\n disconnected user with id! : {$uid} \n");
});

// Example #1

$ns->setup(function($socket){
	// setup socket here
})->run();

// Example #2 
$myserver = $ns->setup(function($socket){
	// setup socket here
});
$myserver->run(); // run socket to accept new connections

$socket->on("test", function($socket, $data){
	// send data to `test` event
	$socket->send("hello test");
	// send data as array to `test` event
	$socket->send(["hello","world","test"]);
});

	$socket->event("neo")->send("hello neo");

$socket->on("test", function($socket, $data){
	// send data to `test` event
	$socket->send("hello test");
	// send data to event type `neo`
	$socket->event("neo")->send("hello neo");
});

	$socket->client("testclient")->send("hello test user");

$socket->on("test", function($socket, $data){
	// send data to `test` event
	$socket->send("hello test event");
	// send data to only `testclient`
	$socket->client("testclient")->send("hello test user");
});

$clients = $socket->getClients();

// for example you want to set first & last name for client
$socket->client("uid")->addAttr("first","test")->addAttr("last","user");

$socket->on("test", function($socket, $data){
	$found_client = false;
	// store selected client in found_client varible
	// and send message to selected client
	$socket->clientByAttr("username","test",$found_client)->send("hello test user");
});

$socket->on("test", function($socket, $data){
	// this line send data to event-type `test`
	$socket->send("send data to test event");
	// this line send data to event-type `neo`
	$socket->event("neo")->send("send data to neo event");
	// because neo is still selected,
	// this line will send data to neo event-type
	$socket->send("send data to neo event");
	// reset references
	$socket->reset();
	// because all selective statements are reset,
	// now send will send data to `test` event-type,
	// because you are calling send method in `test`event
	$socket->send("hello test event-type");
});

$socket->on("test", function($socket, $data){
	// simple example of code chain :)
	$socket->send("send data to test event")
	->event("neo")->send("send data to neo event");
	->send("send data to neo event because `neo` event is selected");
	->client("testuser")
	->send("sending data to `testuser` with event-type `neo`")
	->event("greating")
	->send("sendinig data to `testuser but now with event-type `greating`")
	->reset()
	->send("send data to event-type `test` because references are reset!");
});

	// dismiss client from socket with uid
	$socket->dismiss("testclient");
	// select client then dismiss it
	$socket->client($uid)->dismiss();
	// select client by attribute and dismiss it
	$socket->clientByAttr("username", "test")->dismiss();