Skip to content

Latest commit

 

History

History
116 lines (82 loc) · 2.61 KB

wamp.md

File metadata and controls

116 lines (82 loc) · 2.61 KB
layout title parent nav_order
default
WAMP
Transports
3

{% include support.md %}

Web Application Messaging Protocol (WAMP) Transport

A transport for Web Application Messaging Protocol. WAMP is an open standard WebSocket subprotocol. It uses internally Thruway PHP library thruway/client

Installation

$ composer require enqueue/wamp

Start the WAMP router

You can get a WAMP router with Thruway:

$ composer require voryx/thruway
$ php vendor/voryx/thruway/Examples/SimpleWsRouter.php

Thruway is now running on 127.0.0.1 port 9090

Create context

<?php
use Enqueue\Wamp\WampConnectionFactory;

$connectionFactory = new WampConnectionFactory();

// same as above
$connectionFactory = new WampConnectionFactory('wamp:');
$connectionFactory = new WampConnectionFactory('ws:');
$connectionFactory = new WampConnectionFactory('wamp://127.0.0.1:9090');

$context = $connectionFactory->createContext();

Consume message:

Start message consumer before send message to the topic

<?php
/** @var \Enqueue\Wamp\WampContext $context */

$fooTopic = $context->createTopic('foo');

$consumer = $context->createConsumer($fooQueue);

while (true) {
    if ($message = $consumer->receive()) {
        // process a message
    }
}

Subscription consumer

<?php
use Interop\Queue\Message;
use Interop\Queue\Consumer;

/** @var \Enqueue\Wamp\WampContext $context */
/** @var \Enqueue\Wamp\WampDestination $fooQueue */
/** @var \Enqueue\Wamp\WampDestination $barQueue */

$fooConsumer = $context->createConsumer($fooQueue);
$barConsumer = $context->createConsumer($barQueue);

$subscriptionConsumer = $context->createSubscriptionConsumer();
$subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) {
    // process message

    return true;
});
$subscriptionConsumer->subscribe($barConsumer, function(Message $message, Consumer $consumer) {
    // process message

    return true;
});

$subscriptionConsumer->consume(2000); // 2 sec

Send message to topic

<?php
/** @var \Enqueue\Wamp\WampContext $context */

$fooTopic = $context->createTopic('foo');
$message = $context->createMessage('Hello world!');

$context->createProducer()->send($fooTopic, $message);

back to index