I am using Ratchet and Autobahn.js. I want to make some user validation on subscribe, so I need to pass the session key to the Ratchet WAMP server. Can you tell me how can I pass some data to the server on subscribe event?
I guess you could do an authentication before you proceed to subscribe.
Autobahn has implemented an authentication handshake with WAMP RPCs.
Check the Session Authentication section: http://autobahn.ws/js/reference/#Session_authreq
However, Ratchet hasn't implemented the WAMP CRA protocol, yet. They created a ticket for this about a year ago.
Someone in that ticket has forked Ratchet and implemented it himself.
But it might be easier if you could switch your server to Autobahn Python as it's already supporting the WAMP CRA.
I assume that you are not talking about authentication and that you already have established connection with server.
There is no need for you to pass session id from the client, WAMP take care of that for you. The only information you can pass when subscribing is the topic.
On the php side you can have access to the session id which you can use for validation.
public function onSubscribe(ConnectionInterface $conn, $topic)
{
$sessionId = $conn->WAMP->sessionId
}
Alternative solution: If you really have to pass session id from the client, then you can do something like this:
Javascript:
var appSession = null;
ab.connect(
// The WebSocket URI of the WAMP server
wsuri,
// The onconnect handler
function (session) {
appSession = session;
}
);
appSession.call('myValidationChannelForUser', appSession.sessionid(), 'otherValidationParams').then(function(result)
{
if (result.success)
{
console.log('you have been subscribed to xyz..');
}
}
php:
public function onCall(ConnectionInterface $conn, $id, $fn, array $params)
{
$sessionId = $conn->WAMP->sessionId;
if ($fn == 'myValidationChannelForUser')
{
// validation...
// $params[0] == appSession.sessionid() passed from JS
// $params[1] == otherValidationParams passed from JS
// validation passed, subcribe to channel
if (validated)
{
$this->onSubscribe(ConnectionInterface $conn, $topic);
return $conn->callResult($id, array('success' => 1);
}
}
}