-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from voryx/listen_notify
Add LISTEN/NOTIFY support
- Loading branch information
Showing
8 changed files
with
255 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/bootstrap.php'; | ||
|
||
$client = new PgAsync\Client([ | ||
'host' => '127.0.0.1', | ||
'port' => '5432', | ||
'user' => 'matt', | ||
'database' => 'matt', | ||
]); | ||
|
||
$client->listen('some_channel') | ||
->subscribe(function (\PgAsync\Message\NotificationResponse $message) { | ||
echo $message->getChannelName() . ': ' . $message->getPayload() . "\n"; | ||
}); | ||
|
||
\Rx\Observable::timer(1000) | ||
->flatMapTo($client->query("NOTIFY some_channel, 'Hello World'")) | ||
->subscribe(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace PgAsync\Message; | ||
|
||
class NotificationResponse implements ParserInterface | ||
{ | ||
use ParserTrait; | ||
|
||
private $payload = ''; | ||
|
||
private $notifyingProcessId = 0; | ||
|
||
private $channelName = ''; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function parseMessage(string $rawMessage) | ||
{ | ||
$len = strlen($rawMessage); | ||
if ($len < 10) { | ||
throw new \UnderflowException; | ||
} | ||
|
||
if ($rawMessage[0] !== static::getMessageIdentifier()) { | ||
throw new \InvalidArgumentException('Incorrect message type'); | ||
} | ||
$currentPos = 1; | ||
$msgLen = unpack('N', substr($rawMessage, $currentPos, 4))[1]; | ||
$currentPos += 4; | ||
$this->notifyingProcessId = unpack('N', substr($rawMessage, $currentPos, 4))[1]; | ||
$currentPos += 4; | ||
|
||
$rawPayload = substr($rawMessage, $currentPos); | ||
$parts = explode("\0", $rawPayload); | ||
|
||
if (count($parts) !== 3) { | ||
throw new \UnderflowException('Wrong number of notification parts in payload'); | ||
} | ||
|
||
$this->channelName = $parts[0]; | ||
$this->payload = $parts[1]; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPayload(): string | ||
{ | ||
return $this->payload; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getNotifyingProcessId(): int | ||
{ | ||
return $this->notifyingProcessId; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getChannelName(): string | ||
{ | ||
return $this->channelName; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getMessageIdentifier(): string | ||
{ | ||
return 'A'; | ||
} | ||
|
||
public function getNoticeMessages(): array | ||
{ | ||
return $this->noticeMessages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters