I have a Laravel 5.3 installation running as a pure API application and you need to connect from several different applications.
Everything works fine (after all, this is Laravel, which we are talking about: P), except that I cannot understand one thing:
I have an MQTT server that listens for messages from multiple devices (no matter what). These messages contain information about the class and method of operation that must be called up on the server.
I can’t call the API directly, devices just don’t support this (they do, but it is much more than using MQTT to transfer data). My idea was to direct the new job to a queue that determines which class of Laravel jobs to call (and which method). The problem is serializing JSON ...
The MQTT server is running in NodeJS, my queues are running on Redis. I remember a trick from Taylor where he mentioned that it would theoretically be possible to serialize JSON and push the queue from outside Laravel and handle the work of Laravel.
Does anyone know how to approach this? Is there any documentation available on the JSON structure?
I should also note that this NodeJS push queue solution consumed by working Laravel did not work for me. The same result as above, the job is queued, but discarded without processing or errors.
An example of the data structure for a queue in Redis is as follows:
"{\"job\":\"Illuminate\\\\Broadcasting\\\\BroadcastEvent\",\"data\":{\"event\":\"O:28:\\\"App\\\\Events\\\\NotificationEvent\\\":5:{s:7:\\\"\\u0000*\\u0000name\\\";s:12:\\\"notification\\\";s:4:\\\"data\\\";a:4:{s:4:\\\"testkey\\\";s:14:\\\"testval\\\";s:9:\\\"timestamp\\\";s:19:\\\"2017-02-24 11:07:48\\\";s:5:\\\"event\\\";s:12:\\\"notification\\\";s:5:\\\"class\\\";s:28:\\\"App\\\\Events\\\\NotificationEvent\\\";}s:10:\\\"\\u0000*\\u0000channel\\\";N;s:7:\\\"\\u0000*\\u0000user\\\";O:45:\\\"Illuminate\\\\Contracts\\\\Database\\\\ModelIdentifier\\\":2:{s:5:\\\"class\\\";s:8:\\\"App\\\\User\\\";s:2:\\\"id\\\";i:2;}s:6:\\\"socket\\\";N;}\"},\"id\":\"XuUKRTf8CTSdzaVgp2gRcvmxQqLcpBUG\",\"attempts\":1}"
Based on this structure, I think the object (which should be serialized) should look something like this:
{
"job":"EventClass@method",
"data":{
"event":"EventClass",
"name":"EventName",
"data":{
"key":"value"
"event":"EventName"
"class":"EventClass@method"
}
}
There is additional information about what Laravel actually queues (for example, timestamp, user model identifier, etc.), but I don’t think it is necessary to run the task.
JS , php serialize() ( , php unserialize().
NPM php-serialization ( ), - Laravel (, )
:)
, , Javascript Laravel ( Laravel ).
, Redis. Beanstalkd ( ).
, :
var serialize,Class,job,jobUser,jobData,serialized,result;
serialize = require('php-serialization').serialize;
Class = require('php-serialization').Class;
job = new Class("App\\Events\\NotificationEvent");
job.__addAttr__("name","string","notification","string","protected");
jobData = new Class();
jobData.__addAttr__("testkey","string","testval","string");
jobData.__addAttr__("timestamp","string","2017-02-24 11:07:48","string");
jobData.__addAttr__("event","string","notification","string");
jobData.__addAttr__("class","string","App\\Events\\NotificationEvent","string");
job.__addAttr__("data","string",jobData,"array","public");
job.__addAttr__("channel","string",null,"null","protected");
jobUser = new Class("Illuminate\\Contracts\\Database\\ModelIdentifier")
jobUser.__addAttr__("class","string","App\\User","string","public");
jobUser.__addAttr__("id","string",2,"integer","public");
job.__addAttr__("user","string",jobUser,"object","protected");
job.__addAttr__("socket","string",null,"null","public");
serialized = serialize(job,"object");
result = {
job:"Illuminate\\Broadcasting\\BroadcastEvent",
data:{
event:serialized
},
id:"XuUKRTf8CTSdzaVgp2gRcvmxQqLcpBUG",
attempts:1
};
queue.rpush('queues:default',JSON.stringify(result));
, ID, . , , , . , , ( , Laravel, 32 , , , ).
1. Laravel , .