yii-node-socket is not working for me - php

Recently I have been trying to integrate nodejs with my existing project to make sure of live updating of feeds. Thus, I used the following yii plugin and followed the instruction:
https://github.com/oncesk/yii-node-socket
The host and port are: bigcat,3001 respectively and I have configured them in the console.php and main.php as mentioned. I started the node server and the log indicated that it was listening to:
Listening bigcat:3001
Set origin: bigcat:*
At my client side I have the following codes (i created it as an external javascript):
// create object and connect to web socket server
var socket = new YiiNodeSocket();
socket.on('event.example', function () {
console.log('*****Trigger test.event, is a global event, for all clients*****');
});
socket.onConnect(function () {
socket.room('example').join(function (isConnected, numberOfRoomClients) {
if (isConnected) {
// catch test.event only for this rooom
console.log('***Joined into room example: members count [' + numberOfRoomClients + ']*****');
this.on('example.room.event', function (data) {
console.log('****WORKING****');
});
}
});
});
My console records that the client is successfully connected.An at homeController test method, I have the following code:
$event = Yii::app()->nodeSocket->getFrameFactory()->createEventFrame();
$event->setRoom('example');
$event->setEventName('example.room.event');
$event['type_string'] = 'hello world';
$event['type_array'] = array(1, 2, 3);
$event['type_object'] = array('one' => 1, 'two' => 2);
$event['type_bool'] = true;
$event['type_integer'] = 11;
$event->send();
When I invoke the homeController test method in another browser, my client should be able to listen to the event and the console should print "IT's WORKING". However, that is not happening. My log is showing the following error:
warn: handshake error INVALID SERVER: server host 127.0.0.1 not allowed for /server
debug: websocket writing 7::/server:undefined
The server side codes seems to not work at all. Could I get some advice on how to get it done. I feel I am close and yet far.
The server.config.js setting is:
module.exports = {
host : 'bigcat',
port : parseInt('3001'),
origin : 'bigcat:*',
allowedServers : ["127.0.1.1"],
dbOptions : {"driver":"dummy","config":[]},
checkClientOrigin : 1,
sessionVarName : 'PHPSESSID'
};

I think you need to add 127.0.0.1 in yii config file -> node socket configuration section -> parameter is allowedServerAddresses
Like this^
'nodeSocket' => array(
'class' => 'application.extensions.yii-node-socket.lib.php.NodeSocket',
'host' => 'localhost', // default is 127.0.0.1, can be ip or domain name, without http
'port' => 3001 // default is 3001, should be integer,
<b>'allowedServerAddresses' => array('127.0.0.1')</b>
)
And can you show node-socket/lib/js/server/server.config.js config file?

Related

How to handle multiple environments: dev, staging and production with paypal webhooks?

I would like to have three environments, where I can test and use paypal webhooks. My current approach is to create new webhook for every environment in paypal developer portal and when request arrives check if it is for that environment. How does paypal handle multiple webhook urls and what status code should be returned if environment is not correct?
Create a Class like below to keep in the beginning of your application. Let it initialize once.
class App {
private static $env_status = null;
private static $paypal_settings = [];
const ENV_PRODUCTION = "production";
const ENV_STAGING = "staging";
const ENV_DEV = "development";
public static function init() {
// Set environment status.
// You can also parse domain name and depending on your domain, you can set the environment status.
self::$env_status = getenv("ENV_STATUS"); // getenv() Gets from Environment variable. You'll need set clear_env = no in php config for this to work.
switch(self::$env_status) {
case App::ENV_PRODUCTION:
self::$paypal_settings = [
"mode" => "live"
"clientID" => "PRODUCTION_CLIENT_ID" ,
"secret" => "PRODUCTION_SECRET" ,
"currency" => "USD",
"webhook" => "https://example.com/live_webhook_endpoint"
];
break;
case App::ENV_STAGING:
self::$paypal_settings = [
"mode"=> "sandbox"
"clientID"=> "STAGING_CLIENT_ID" ,
"secret"=> "STAGING_SECRET" ,
"currency"=> "USD",
"webhook" => "https://example.com/staging_webhook_endpoint"
];
break;
default:
// ENV_DEV settings
self::$paypal_settings = [
"mode"=> "sandbox"
"clientID"=> "DEVELOPMENT_CLIENT_ID" ,
"secret"=> "DEVELOPMENT_SECRET" ,
"currency"=> "USD",
"webhook" => "https://example.com/development_webhook_endpoint"
];
break;
}
}
public static function env_status() {
return self::$env_status;
}
public static function paypal_settings() {
return self::$paypal_settings;
}
// You can also create seprate function if you just want webhook URL.
// You can define in different variable also if that's the case.
public static function paypal_webhook_url() {
return self::$paypal_settings['webhook'];
}
} App::init();
Then whenever you want to get paypal settings you can call it from anywhere in your Application.
$paypay_settings = App::paypal_settings();
OR if you need just paypal webhook URL
$paypal_webhook_url = App::paypal_webhook_url();
This way you don't have to keep any conditions in other parts of your code. All the conditions will go in a single place, which will be easier to update later.
How does paypal handle multiple webhook urls.
You will need to hit PayPal Sandbox URL to hit for staging/development environment.
What status code should be returned if environment is not correct?
HTTP 400. Since it will be an invalid request.
Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
On one of our projetcs, we use different configuration variables depending on the environnement.
For dev
$config['paypal.settings'] = array(
'mode'=> "sandbox", //'live' or 'sandbox'(default)
'clientID'=>"CLIENT_ID_FOR_DEV" ,
'secret'=> "SUPERSECRETCLIENT_SECRET_FOR_DEV" ,
'currency'=>'EUR',
'logEnabled' => false,
'logDir'=>__DIR__ . '/../logs'
);
For prod :
$config['paypal.settings'] = array(
'mode'=> "live",
'clientID'=>"CLIENT_ID_FOR_PROD" ,
'secret'=> "SUPERSECRETCLIENT_SECRET_FOR_PROD" ,
'currency'=>'EUR',
'logEnabled' => false,
'logDir'=>__DIR__ . '/../logs'
);
and our dev and prod environnement are obviously on 2 different domains that are configured for each CLIENT_ID on the paypal interface.
In the webhook controller called by Paypal we have :
class WebhookController{
function paypalPingBackAction($request){
$paypalSettings = //get paypal settings
$isLive = ($paypalSettings["sandbox"] ?? "sandbox") == "live";
$currentDomain = $request->getDomain();
// now we have enough information (domain and sandbox)
// to do things accordingly
}
}
Paypal does not really care about what code you send back. But if you send a 500, he will retry later. So maybe, if things go well, just return an empty 201 accepted !

how to send message remotely to kafka

I am very new to kafka. I am trying to send a message from my local machine producer to kafka server. I am not able to figure out the issue or what am doing worng.
$config = \Kafka\ProducerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('localhost:9092');
$config->setBrokerVersion('1.0.0');
$config->setRequiredAck(1);
$config->setIsAsyn(false);
$config->setProduceInterval(500);
$producer = new \Kafka\Producer(
function() {
return [
[
'topic' => 'test',
'value' => 'test....message.',
'key' => 'testkey',
],
];
}
);
// $producer->setLogger($logger);
$producer->success(function($result) {
print_r($result);
});
$producer->error(function($errorCode) {
var_dump($errorCode);
});
$producer->send(true);
Output:-
Fatal error: Uncaught exception 'Kafka\Exception' with message 'Not has broker can connection metadataBrokerList' in C:\xampp\htdocs\vendor\nmred\kafka-php\src\Kafka\Producer\Process.php on line 193
So you're trying to produce from your local machine to a different server? If that's the case you'll need to update the line
$config->setMetadataBrokerList('localhost:9092');
to point to that server's domain name and not localhost:9092
This is the library https://github.com/weiboad/kafka-php I was using in codeigniter for the producer.
This library works fine.
The issue was that on server port number was changed, it was 29092 actually by default the port is 9092 that why the connection was failing.

RabbitMQ receives message but it goes nowhere

I have a RabbitMQ installation on a PuPHPet-generated virtual box (Ubuntu 16.04 x64) on top of Windows 10.
After the setup was complete, I configured a new user using rabbitmqctl:
# rabbitmqctl add_user root root
# rabbitmqctl set_permissions -p / root ".*" ".*" ".*"
# rabbitmqctl set_user_tags root administrator
Following the PHP & RabbitMQ Tutorial, I set up a sender and receiver script.
The sender script is the following:
<?php
require_once 'vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 5672, 'root', 'root');
$channel = $connection->channel();
$channel->queue_declare('my_queue', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');
echo "Sent 'Hello World!'\n";
$channel->close();
$connection->close();
And the receiver script is the following:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'root', 'root');
$channel = $connection->channel();
$channel->queue_declare('my_queue', false, false, false, false);
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$callback = function($msg) {
echo " [x] Received ", $msg->body, "\n";
};
$channel->basic_consume('my_queue', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>
I opened a screen for the receiver script and fired off a few executions of the sender script:
$ php sender.php
Sent 'Hello World!'
The sender script didn't run into any errors (and I was able to verify that the queue was declared in rabbit), but the receiver did not output that it had received / consumed any messages.
Furthermore, a quick check using the admin manager plugin shows that the queue has no messages at all:
$ curl -i -u root:root http://localhost:15672/api/queues
...
{
"messages_details":{
"rate":0.0
},
"messages":0,
"messages_unacknowledged_details":{
"rate":0.0
},
"messages_unacknowledged":0,
"messages_ready_details":{
"rate":0.0
},
"messages_ready":0,
"reductions_details":{
"rate":0.0
},
"reductions":9294,
"node":"rabbit#leon",
"arguments":{
},
"exclusive":false,
"auto_delete":false,
"durable":false,
"vhost":"/",
"name":"my_queue",
"message_bytes_paged_out":0,
"messages_paged_out":0,
"backing_queue_status":{
"avg_ack_egress_rate":0.0,
"avg_ack_ingress_rate":0.0,
"avg_egress_rate":0.0,
"avg_ingress_rate":0.0,
"delta":[
"delta",
"undefined",
0,
0,
"undefined"
],
"len":0,
"mode":"default",
"next_seq_id":0,
"q1":0,
"q2":0,
"q3":0,
"q4":0,
"target_ram_count":"infinity"
},
"head_message_timestamp":null,
"message_bytes_persistent":0,
"message_bytes_ram":0,
"message_bytes_unacknowledged":0,
"message_bytes_ready":0,
"message_bytes":0,
"messages_persistent":0,
"messages_unacknowledged_ram":0,
"messages_ready_ram":0,
"messages_ram":0,
"garbage_collection":{
"minor_gcs":12,
"fullsweep_after":65535,
"min_heap_size":233,
"min_bin_vheap_size":46422,
"max_heap_size":0
},
"state":"running",
"recoverable_slaves":null,
"consumers":0,
"exclusive_consumer_tag":null,
"effective_policy_definition":[
],
"operator_policy":null,
"policy":null,
"consumer_utilisation":null,
"idle_since":"2018-01-28 15:21:22",
"memory":9640
},
...
It looks like the message is accepted but is immediately discarded & not logged. Speaking of logs, there's nothing suspicious in the rabbit log either. Just a lot of this:
2018-01-28 15:47:43.654 [info] <0.1417.0> accepting AMQP connection <0.1417.0> ([::1]:49058 -> [::1]:5672)
2018-01-28 15:47:43.696 [info] <0.1417.0> connection <0.1417.0> ([::1]:49058 -> [::1]:5672): user 'root' authenticated and granted access to vhost '/'
2018-01-28 15:47:43.742 [info] <0.1417.0> closing AMQP connection <0.1417.0> ([::1]:49058 -> [::1]:5672, vhost: '/', user: 'root')
Why aren't the messages coming through?
Your code seems perfectly fine except for that you have sent the message to a wrong queue. When you send a message to a queue that doesn't exist, RabbitMQ will simply discard the message since it doesn't know where to send it.
In your code, you use the default exchange to send the message. That is:
$channel->basic_publish($msg, '', 'hello');
Here we use the default or nameless exchange: messages are routed to
the queue with the name specified by routing_key, if it exists. The
routing key is the third argument to basic_publish
https://www.rabbitmq.com/tutorials/tutorial-three-php.html
so when you use the default exchange, you have to specify the routing key as the third parameter, which is your queue name. RabbitMQ creates routing keys with the same name as queues when you use the default exchange.
To fix your code, simply change hello to your queue name, i.e my_queue and it will start sending and receiving.
Hope it helps :)
The RabbitMQ team monitors this mailing list and only sometimes answers questions on StackOverflow.
Since you did not bind my_queue to any exchange, you must publish to the default exchange using my_queue as the routing key. All queues are bound to the default topic exchange using the queue name as the routing key.
In your code, you are using hello as the routing key.

Error 99 - permission denied in last.fm php API call

I'm making a call to retrieve a user's latest tracks using the last.fm PHP api library. It works perfectly when I run it on my local webserver (localhost), but when I run it on a remote server it sends back an error code of 99, saying that permission has been denied.
Here is my code:
static function readRecentTracks() {
$authVars['apiKey'] = '#########';
$auth = new lastfmApiAuth('setsession', $authVars);
$apiClass = new lastfmApi();
$packageClass = $apiClass->getPackage($auth, 'user');
$method_vars = array(
'user' => 'liquidus219',
'limit' => 25
);
if ($tracks = $packageClass->getRecentTracks($method_vars)) {
echo json_encode($tracks);
} else {
echo '<b>Error '.$packageClass->error['code'].' - </b><i>'.$packageClass->error['desc'].'</i>';
}
}
You can see the code in action at http://liquidus219.freehostia.com/api/?q=readRecentTracks
UPDATE: I have checked/changed the application URL to no effect.
UPDATE: I have gone through the code for the last.fm PHP api, no sign of an error 99 defined anywhere.
I found out in the end that my hosting provider was blocking the call.

Trigger ISPConfig vhost folders creation

I make an script that fill up all ISPConfig tables by itself and now i only need to raise some script to create the needed vhost and the rest of the symblink needed for apache to work.
My Script is working like a charm since i can view all the data correctly using the ISPConfig frontend.
Digging into the ISPConfig panel i see a RaiseEvent function triggered everytime a record is created but i can't trace where it ends and how it perform the creation of the symblink.
Maybe calling some function or cron it can work.
I'm using Apache 2 + PHP 5.3 + MySQL + ISPConfig 3 on Ubuntu Server 10.4
Ok I respond myself.
Since version 3 ISPConfig came with a simple API that let you performe some operation like Adding FTP Users, Websites and Databases.
I left here an example of how to create a database:
$params_db = array(
'server_id' => '1',
'system_user' => "web10",
'system_group' => 'client0',
'active' => 'y',
'type' => 'mysql',
'database_name' => $NAME,
'database_user' => $NAME,
'database_password' => '123456',
'database_charset' => 'utf8',
'remote_access' => 'n',
);
Next we have to create on the ISPConfig panel a "remote user" that allow to comunicate using the webservice.
$soap_username = 'whatever';
$soap_password = 'h4ck3m3';
$soap_location = 'http://localhost:8080/remote/index.php';
$soap_uri = 'http://localhost:8080/remote/';
$client = new SoapClient(null, array('location' => $soap_location, 'uri' => $soap_uri));
So, what's next?
Next we call the webserver function like this:
try
{
//* Login to the remote server
if( $session_id = $client->login($soap_username,$soap_password))
{
echo 'Logged into remote server sucessfully. The SessionID is '.$session_id. "\n";
$client->sites_database_add($session_id, $client_id, $params_db);
//* Logout
if($client->logout($session_id))
{
echo "DB Created\n";
}
}
}
catch (SoapFault $e)
{
die('SOAP Error: '.$e->getMessage());
}
For more information check out this link of howtogeek website: http://www.howtoforge.com/how-to-create-remote-api-scripts-for-ispconfig-3

Categories