PHP Real Time chat with Ratchet Websockets - php

I'm a beginner in PHP Websockets and I'm trying to create real-time chat with database storage. I was doing pretty good, but now I'm standing at one problem. There's problem, when user1 sends message to user2 and user2 came to the site first (reload first on localhoste), it won't be "real-time".
Let me explain it further.
Here's my server.php. It is practicly the same as ratchet tutorial:
$loop = React\EventLoop\Factory::create();
$pusher = new \Pusher();
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer($pusher ))), $webSock);
$loop->run();
In pusher.php are most important these methods (I omitted other non-important stuff):
protected $subscribedTopics = array();
protected $myID = array();
public function onSubscribe(ConnectionInterface $conn, $data) {
$this->subscribedTopics[json_decode($data)->teamID] = $data;
$this->myID[json_decode($data)->userID] = $data;
}
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
if ((!array_key_exists($entryData['team_id'], $this->subscribedTopics)) ||
(!array_key_exists($entryData['to_user_id'], $this->myID))
) {
return;
}
$teamID = $this->subscribedTopics[$entryData['team_id']];
$teamID->broadcast($entryData);
}
In my presenter Class I have simple form. When user submits this form, this code follows:
$this->chatPartner = $values['to_user_id']; //this I get from the form
$this->redrawControl('msg'); //here I redraw my layout
$this->messages_model->addMessage($values); //here I send data to database
$context = new \ZMQContext();
$socket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($values));
Then, in view I have this JavaScript code:
var myJSON = '{'
+ '"teamID" : {$teamId},' //this I get from the presenter
+ '"userID" : {$userId}' //this I get from the presenter
+ '}';
var conn = new ab.Session('ws://localhost:8080',
function() {
conn.subscribe(myJSON, function(topic, data) {
if (data.from_user_id == mypartnerIdA) {
//here I edit the DOM
}
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
So, back to my problem. I simulate 2 users. User1 reloads this page, where is javascript connection first. User2 reloads this page after him. When User1 sends a message to user2, message appers immediatly (real-time). But when user2 sends a message to user1, this message doesn't appear immediatly - it appears only after next reload of the page.
And my question is - How to fix this? How to make user2's message real-time, too? How can I fix this my code?

You probably have a misunderstanding of what the data yous end to subscribe is.
It is meant to use for ID's of chat sessions.
for example:
A has a chat with B (chatId = 1)
B has a chat with C (chatId = 2)
C has a chat with A (chatId = 3)
A, B and C are in one chat (chatId = 4)
var chatId = 2; //this chat is only between users B and C
conn.subscribe( chatId , function(topic, data) {
...
}
The easiest way for me to understand it was by comparing it to a hashtag on twitter.
Each hashtag is in your case a chatId.
And for each subscription to a hashtag/chatId. You will have a WebSocket connection so that you receive all the updates for it.
This will be an easier way to do this in the long run then by having subdivided connections for a userId parameter.
It can also easily be stored in a database so that you know whom to send the messages to and who not.

Related

Ratchet Push Server Tutorial issues

I am trying to get the ratchet push tutorial to work.
http://socketo.me/docs/push
I am doing exactly what the tutorial is saying but no messages will be received by my subscriber.
My server.php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
My addblog.php
<?php
// post.php ???
// This all was here before ;)
$entryData = array(
'category' => 'kittensCategory'
, 'title' => 'Test'
, 'article' => 'Test'
, 'when' => time()
);
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($entryData));
And my listener.html
<script src="autobahn.js"></script>
<script>
var conn = new ab.Session('ws://localhost:8080',
function() {
conn.subscribe('kittensCategory', function(topic, data) {
// This is where you would add the new article to the DOM (beyond the scope of this tutorial)
console.log('New article published to category "' + topic + '" : ' + data.title);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>
And my Pusher.php
<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class Pusher implements WampServerInterface {
/**
* A lookup of all the topics clients have subscribed to
*/
protected $subscribedTopics = array();
public function onSubscribe(ConnectionInterface $conn, $topic) {
echo "Hello to: ".$topic;
$this->subscribedTopics[$topic->getId()] = $topic;
}
/**
* #param string JSON'ified string we'll receive from ZeroMQ
*/
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
echo "gallogallo";
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['category'], $this->subscribedTopics))
{
return;
}
$topic = $this->subscribedTopics[$entryData['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
I do not receive any errors or warnings.
When I try to call addblog.php nothing is happening and I do not understand why.
Any hints maybe? I am testing this with XAMPP and Windows 10.
I don't know why, but it is working very well on another system...

websocket with codeigniter and android

i have a web application developed in codeigniter php and an android app for event management what i want to do is that whenever admin on web create an event a notification should be generated and shown into android app so all the user with android app can receive that notification without any interrupt.
so any one have idea how i can achieve this feature??
i am thinking of using web socket but i dont have any idea about it in codeigniter and android so any kind of suggestion will be helpful.
I would use the following components:
ZeroMQ for passing the messages http://zeromq.org
Ratchet for web-socket server http://socketo.me
Autobahn for web-socke client http://autobahn.ws/android/
I have no clue as to what you could use on the android side to subscribe to the web-service server.
The code is fairly simple, here is an example of something i use in my project.
Web-socket server:
composer.json
{
"autoload": {
"psr-0": {
"MyApp": "src"
}
},
"require": {
"cboden/ratchet": "0.3.*",
"react/zmq": "0.2.*|0.3.*"
}
}
push-server.php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
// I assume the codeigniter installation and this server
// will be on the same host, hence 127.0.0.1
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($pusher, 'onMessage'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8081, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
), $webSock
);
$loop->run();
Then in codeigniter you can use the following to send messages:
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'pusher');
$zmq_srv = 'your.domain.com:5555';
$socket->connect("tcp://" . $zmq_srv);
$messageContent = array(
'user' => 'username',
'type' => 'success',
'message' => 'Hi this is a test message.',
);
$socket->send(json_encode($messageContent));
I use this above to send messages to particular user, but if you make a new channel to which all your users are connected then all of them would receive a message.
My web based app uses http://autobahn.ws/js/ in the views to subscribe to the web-socket feeds. I see it has android implementation as well, but i've never tried that one: http://autobahn.ws/android/
This is the sample code from one of my views in case it is useful to you:
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
var conn = new ab.Session('ws://your.domain.com:8081',
function () {
// Subscribe to the "username" channel
// For each user this would be their own channel to receive notifications
// for their own events, like successful file generation..
// file upload, etc...
conn.subscribe('username', function (topic, data) {
$.simplyToast(data.message, type = data.type, delay = 8000);
});
// Subscribe to "system" channel.
//In my app all users are subscribed to this one to receive system-wide
// notifications.
conn.subscribe('system', function (topic, data) {
$.simplyToast(data.message, type = data.type, delay = 8000);
});
},
function () {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>

Issues with Ratchet WAMP Component

Brief Explanation...
I am trying to set up a Ratchet Web Socket server and am having a few issues. Here is what I am trying to achieve:
I have a counter on my website that indicates how many users have signed up to the website. I would like this counter to update whenever another users signs up to the website. Simples...
What I have Tried
I have only been using Ratchet for around 24 hours so my experience is extremely limited to say the least, but nevertheless, I have read the documentation thoroughly and I believe I am on the right track.
Here is my code:
push-server.php
// Autoload any required libraries
require(dirname(__DIR__).'/vendor/autoload.php');
// Initiate the loop and pusher
$loop = React\EventLoop\Factory::create();
$pusher = new _sockets\pusher;
// Listen for the web server to make a ZeroMQ push
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
// Binding to 127.0.0.1 means the only client that can connect is itself
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($pusher,'message'));
// Set up the web socket server for the clients
$web_socket = new React\Socket\Server($loop);
// Binding to 0.0.0.0 means remotes can connect
$web_socket->listen(8080,'0.0.0.0');
$web_server = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$web_socket
);
// Run the loop
$loop->run();
pusher.php
namespace _sockets;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class pusher implements WampServerInterface {
/**
* A lookup of all the topics clients have subscribed to
*/
protected $subscribedTopics = array();
public function onSubscribe(ConnectionInterface $conn, $topic) {
$this->subscribedTopics[$topic->getId()] = $topic;
}
public function onUnSubscribe(ConnectionInterface $conn, $topic){
}
public function onOpen(ConnectionInterface $conn){
}
public function onClose(ConnectionInterface $conn){
}
public function onCall(ConnectionInterface $conn, $id, $topic, array $params){
}
public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible){
}
public function onError(ConnectionInterface $conn, \Exception $e){
}
public function message($data){
$data = json_decode($data,true);
// If the lookup topic object isn't set there is no one to publish to
if(!array_key_exists($data['category'],$this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$data['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($data);
}
}
Snippet from Sign Up Script
// Push the sign up notice to all connections
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH);
$socket->connect("tcp://localhost:5555");
$array = array(
'category' => 'user_signed_up'
);
$socket->send(json_encode($array));
Snippet from JavaScript
// Connect to the website
var connection = new ab.Session('ws://MY_IP_ADDRESS:8080',
function(){
console.log('Connected to WebSocket');
connection.subscribe('user_signed_up',function(topic,data){
console.log(topic,data);
});
},
function(){
console.log('WebSocket Connection Closed');
},
{
'skipSubprotocolCheck': true
}
);
My Questions
All of the above works but before I proceed to thorough testing and production, I have a couple of questions:
Is the $persistent_id necessary with the getSocket method? I have read documentation but what is it actually used for? Do I need it, or, should I be using it?
I have been unable to find documentation on the on method for the ZMQ\Context class, has this been deprecated? Should I be using this or instead should I use recv?
How can I ensure my push-server.php is running all the time? Is there some sort of daemon tool I can use to ensure it is always running and will auto start if the server is rebooted?
Is it possible to attach the websocket to my domain instead of my IP Address? When I use my domain within the JavaScript, I receive a 400 Error...

PHP WebSocket ZMQ - Chat Operation - Send data to specific user

im working on a PHP project based on Symfony 2.2.11 and I installed the socketo related to the following tutorial http://socketo.me/docs/install to make my chat script working.
ServerCommand.php // Code of the command line that starts the WebSocket server
$oLoop = Factory::create();
// Listen for the web server to make a ZeroMQ push after an ajax request
$oContext = new Context($oLoop);
$oPull = $oContext->getSocket(\ZMQ::SOCKET_PULL);
// LET IT 127.0.0.1
$oPull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$oPull->on('message', array($oChat, 'onMessage'));
// Set up our WebSocket server for clients wanting real-time updates
$oWebSock = new Server($oLoop);
$oWebSock->listen(7979, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new IoServer(
new HttpServer(
new WsServer(
new WampServer(
$oChat
)
)
),
$oWebSock
);
$oLoop->run();
After a message is being added to database :
MessagesController.php
....
// This is our new stuff
$oContext = new \ZMQContext();
$oSocket = $oContext->getSocket(\ZMQ::SOCKET_PUSH, 'PushMe');
$oSocket->connect("tcp://mydomain:5555");
$aData = array(
'topic' => 'message',
'sUsername' => $oUserCurrent->getUsername(),
'sMessage' => $sMessage
);
$oSocket->send(json_encode($aData));
.....
The chat service :
Chat.php
/**
* A lookup of all the topics clients have subscribed to
*/
public function onSubscribe(ConnectionInterface $conn, $topic)
{
// When a visitor subscribes to a topic link the Topic object in a lookup array
$subject = $topic->getId();
$ip = $conn->remoteAddress;
if (!array_key_exists($subject, $this->subscribedTopics))
{
$this->subscribedTopics[$subject] = $topic;
}
$this->clients[] = $conn->resourceId;
echo sprintf("New Connection: %s" . PHP_EOL, $conn->remoteAddress);
}
/**
* #param string JSON'ified string we'll receive from ZeroMQ
*/
public function onMessage($jData)
{
$aData = json_decode($jData, true);
var_dump($aData);
if (!array_key_exists($aData['topic'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$aData['topic']];
// This sends out everything to multiple users, not what I want!!
// re-send the data to all the clients subscribed to that category
$topic->broadcast($aData);
}
JS code that receives data :
messages.html.twig :
var conn = new ab.Session(
'ws://mydomain:7979' // The host (our Ratchet WebSocket server) to connect to
, function() { // Once the connection has been established
conn.subscribe('message', function(topic, data)
{
console.log(topic);
console.log(data);
});
}
, function() { // When the connection is closed
console.warn('WebSocket connection closed');
}
, { // Additional parameters, we're ignoring the WAMP sub-protocol for older browsers
'skipSubprotocolCheck': true
}
);
So everytings working perfectly, when I send a new Message, it goes to DB then it lands on the page of the chat.
PROBLEM :
The data lands wherever the JS script is, and the result is that all users can get the same recorded message
ASKING :
How can I make data lands in a specific user page ?
Thank you
You are using Ratchet on backend side, right?
So, here you have very good example of case you need:
http://socketo.me/docs/hello-world
You should keep your client connections inside $clients property (not collection of resources id!). So, you can choose one element from this collection and send a message only to this client.
Example:
public function onSubscribe(ConnectionInterface $conn, $topic)
{
// When a visitor subscribes to a topic link the Topic object in a lookup array
$subject = $topic->getId();
$ip = $conn->remoteAddress;
if (!array_key_exists($subject, $this->subscribedTopics))
{
$this->subscribedTopics[$subject] = $topic;
}
$this->clients[] = $conn; // you add connection to the collection
$conn->send("Hello new user!"); // you send a message only to this one user
}

NodeJS AMQP Client can't connect

I am going crazy for last two days, i cannot make connection on NodeJS client with durable exchange and durable queue.
So PHP code creates and send message:
<?php
$connection = new AMQPConnection(array(
'host' => 'localhost',
'vhost' => 'bvh',
'port' => 5672,
'login' => 'bizneus',
'password' => 'lozinkus'
));
//$connection = new AMQPConnection();
$connection->connect();
if (!$connection->isConnected()) {
die('Not connected :(' . PHP_EOL);
}
// Open Channel
$channel = new AMQPChannel($connection);
// Declare exchange
$exchange = new AMQPExchange($channel);
$exchange->setName('biznea_e_1');
$exchange->setType('fanout');
$exchange->setFlags(AMQP_DURABLE);
$exchange->declare();
// Create Queue
$queue = new AMQPQueue($channel);
$queue->setName('notify');
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$message = $exchange->publish(json_encode($s), 'kljuc');
if (!$message) {
echo 'Message not sent', PHP_EOL;
} else {
echo 'Message sent!', PHP_EOL;
}
if ($connection->isConnected()) {
$connection->disconnect();
}
On screen it says that messege is sent.
Next thing is NodeJS client, which should get messages, but it can't:
var amqp = require('amqp');
var conParam = {
host: 'localhost',
port: 5672,
login: 'bizneus',
password: 'lozinkus',
vhost: 'bvh'
}
var connection = amqp.createConnection(conParam);
connection.on('ready', function(){
var exchange = connection.exchange('biznea_e_1');
var queue = connection.queue('notify');
queue.bind('biznea_e_1', 'kljuc');
queue.subscribe( {ack:true}, function(message){
var dj = JSON.parse(message.data.toString());
console.log(JSON.stringify(dj));
queue.shift();
});
});
but I get this error
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: PRECONDITION_FAILED - cannot redeclare exchange 'biznea_e_1' in vhost 'bvh' with different type, durable, internal or autodelete value
at Exchange._onMethod (/home/zijad/node_modules/amqp/amqp.js:1824:15)
at Exchange.Channel._onChannelMethod (/home/zijad/node_modules/amqp/amqp.js:1365:14)
at Connection._onMethod (/home/zijad/node_modules/amqp/amqp.js:922:28)
at AMQPParser.self.addListener.parser.onMethod (/home/zijad/node_modules/amqp/amqp.js:797:12)
at AMQPParser._parseMethodFrame (/home/zijad/node_modules/amqp/amqp.js:442:10)
at frameEnd (/home/zijad/node_modules/amqp/amqp.js:187:16)
at frame (/home/zijad/node_modules/amqp/amqp.js:172:14)
at AMQPParser.header [as parse] (/home/zijad/node_modules/amqp/amqp.js:159:14)
at AMQPParser.execute (/home/zijad/node_modules/amqp/amqp.js:231:21)
at Connection.<anonymous> (/home/zijad/node_modules/amqp/amqp.js:837:12)
I tried to remove var exchange = connection.exchange('biznea_e_1'); that line but than it cannot declare queue.
I just want to send messages from PHP to NodeJS deamon and that is all!
Help :)
Try this: In the node.js code, declare the exchanges and queues with EXACTLY the same parameters as you did in your PHP code. e.g. durable. This may solve your problem.
Cheers!
Looks like you are trying to create the exchange 'biznea_e_1' again in the node.js code. It is already created by the php code. Try only to subscribe.

Categories