PHPackages                             mzohaibnaz/neosocket - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. mzohaibnaz/neosocket

ActiveLibrary[HTTP &amp; Networking](/categories/http)

mzohaibnaz/neosocket
====================

Easy and simple library to create and manage web sockets.

1.0(6y ago)14MITPHPPHP &gt;=7.1.7

Since Nov 2Pushed 6y ago1 watchersCompare

[ Source](https://github.com/mzohaibnaz/neosocket)[ Packagist](https://packagist.org/packages/mzohaibnaz/neosocket)[ Docs](https://github.com/mzohaibnaz/neosocket)[ RSS](/packages/mzohaibnaz-neosocket/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

NeoSocket - (PHP)
=================

[](#neosocket---php)

NeoSocket is a very simple and lite library that can help you to manage your socket logics. if you are using NeoSocket you should use [NeoSocket - JS Client](https://github.com/mzohaibnaz/ns-jsclient) for complete solution package.

Installation
------------

[](#installation)

- Using composer

```
$ composer install mzohaibnaz/neosocket
```

How to use
----------

[](#how-to-use)

- [Initialization](#initializing-neosocket)
- [Setup socket](#setup-your-socket)
- - [Change host &amp; port](#binding-socket-to-hostport)
- [Setup events](#setup-events)
- - [Default event-types](#default-event-types)
- - [New Connection](#new-connection)
- - [On Disconnect](#on-user-disconnected)
- [Run Socket](#run-socket-after-setup)
- [Sending data](#data-sending)
- - [Send to other event](#send-data-to-other-event)
- - [Send to client](#send-data-to-specific-client)
- [Get all clients](#get-all-clients-with-attributes)
- - [Add attributes](#set-client-custom-attributes)
- - [Get client by attribute](#get-client-by-attribute)
- [TIP\* : how to do code chain](#how-to-do-code-chain)
- [Dismiss client](#dismiss-client)

Initializing NeoSocket
----------------------

[](#initializing-neosocket)

Initializing `NeoSocket` using `SocketManager` class

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

Setup your socket
-----------------

[](#setup-your-socket)

`setup` method will bind your socket on host localhost with port 6940 `setup` take 1 `parameter` as callback function

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

##### Binding Socket to host/port

[](#binding-socket-to-hostport)

bind socket on different host and port using `create` method `create` take 2 `parameter` as (host, port)

```
$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");
});
```

Setup events
------------

[](#setup-events)

After socket is setup now setup all your events inside it. For setup events use `on` method. `on` method take 2 `parameter` (event\_name, callback\_function)

```
$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);
});
```

###### setup event with anonymous function

[](#setup-event-with-anonymous-function)

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

#### Default Event Types

[](#default-event-types)

`NeoSocket` library using 2 event types as default types to notify develop for new connection and disconnection of user.

- `connection` for new connection
- `disconnected` for disconnection of user

##### New Connection

[](#new-connection)

`connection` event-type take 2 parameters in callback

- socket reference
- uid of new connection `auto-generated`

###### Example for new connection

[](#example-for-new-connection)

```
	// 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);
});
```

##### On user disconnected

[](#on-user-disconnected)

`disconnected` event-type take 2 parameters in callback

- socket reference
- uid of disconnected user

###### Example for disconnected

[](#example-for-disconnected)

```
$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");
});
```

Run socket after setup
----------------------

[](#run-socket-after-setup)

`run` method is used to actually run your socket server after all events are setup.

###### Example

[](#example)

```
// 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
```

Data Sending
------------

[](#data-sending)

`send` method is used to send data on events. `send` method take 1 `parameter` as data ( `string / array` )

```
$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"]);
});
```

##### \# send data to other event

[](#-send-data-to-other-event)

`event` method used to select `event-type` before sending data on it. `event` method take 1 parameter as event type

- ###### Example Code

    [](#example-code)

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

- ###### Full code

    [](#full-code)

```
$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");
});
```

##### \# send data to specific client

[](#-send-data-to-specific-client)

`client` method used to select `client` before sending data on it. `client` method take 1 parameter as client `uid`

- ###### Example Code

    [](#example-code-1)

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

- ###### Full code

    [](#full-code-1)

```
$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");
});
```

### Get all clients with attributes

[](#get-all-clients-with-attributes)

`getClients` is used to get list of all active clients in socket with their attributes

```
$clients = $socket->getClients();
```

#### Set client custom attributes

[](#set-client-custom-attributes)

`addAttr` will help you to add attributes to your client object for additional information storage. `addAttr` take 2 parameters as `key` and `value` of an attribute.

> Note: before adding attribute select client by [client](#send-data-to-specific-client) or [clientByAttr](#get-client-by-attribute) method.

- ##### Example code

    [](#example-code-2)

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

#### Get client by attribute

[](#get-client-by-attribute)

`clientByAttr` is used to select client like [client](#send-data-to-specific-client) `method` but by its attribute value. `clientByAttr` takes 3 parameters as mention below

- `key` in which you want to search
- `value` value of that key
- `reference`variable to store searched client . `optional parameter`

> Note: if searched result is multiple code will select the very first matched client.

##### \# Example Code

[](#-example-code)

```
$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");
});
```

### Reset Instance References

[](#reset-instance-references)

`reset` method is used to reset selected event/client for that current socket reference within on method

- when you call event/client method to select that selective statement remain until you call reset method. this can help you to perform chain actions. like example below
- ##### Code Example

    [](#code-example)

```
$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");
});
```

#### How to do code chain

[](#how-to-do-code-chain)

```
$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

[](#dismiss-client)

`dismiss` method used to disconnect `client` from socket. `dismiss` method take 1 parameter as client `uid`

- ###### Example Code

    [](#example-code-3)

```
	// 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();
```

License
=======

[](#license)

[MIT](https://choosealicense.com/licenses/mit/)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 77.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2380d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10415131?v=4)[M Zohaib Naz](/maintainers/mzohaibnaz)[@mzohaibnaz](https://github.com/mzohaibnaz)

---

Top Contributors

[![mzohaibnaaz](https://avatars.githubusercontent.com/u/72439265?v=4)](https://github.com/mzohaibnaaz "mzohaibnaaz (7 commits)")[![mzohaibnaz](https://avatars.githubusercontent.com/u/10415131?v=4)](https://github.com/mzohaibnaz "mzohaibnaz (2 commits)")

---

Tags

socketsWebSocketsSocket.iosimplesockets

### Embed Badge

![Health badge](/badges/mzohaibnaz-neosocket/health.svg)

```
[![Health](https://phpackages.com/badges/mzohaibnaz-neosocket/health.svg)](https://phpackages.com/packages/mzohaibnaz-neosocket)
```

###  Alternatives

[cboden/ratchet

PHP WebSocket library

6.4k21.4M238](/packages/cboden-ratchet)[rmccue/requests

A HTTP library written in PHP, for human beings.

3.6k34.5M253](/packages/rmccue-requests)[ratchet/rfc6455

RFC6455 WebSocket protocol handler

23433.7M69](/packages/ratchet-rfc6455)[voryx/thruway-bundle

WebSockets (WAMP2) integration for Symfony2

98120.8k](/packages/voryx-thruway-bundle)[chrome-php/wrench

A simple PHP WebSocket implementation

673.7M4](/packages/chrome-php-wrench)[elephantio/elephant.io

Send events to a socket.io server through PHP

135713.8k7](/packages/elephantio-elephantio)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
