I started the Mercure hub with this command on Windows10 :
mercure --jwt-key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOltdfX0.Oo0yg7y4yMa1vr_bziltxuTCqb8JVHKxp-f_FwwOim0" --addr="localhost:3000" --allow-anonymous --cors-allowed-origins="*"
The hub run successfully
I want to publish updates from Symfony :
In the .env file there is :
###> symfony/mercure-bundle ###
# See https://symfony.com/doc/current/mercure.html#configuration
MERCURE_PUBLISH_URL=http://localhost:3000/.well-known/mercure
# The default token is signed with the secret key: !ChangeMe!
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOltdfX0.Oo0yg7y4yMa1vr_bziltxuTCqb8JVHKxp-f_FwwOim0
###< symfony/mercure-bundle ###
In the lexik_jwt_authentication.yaml
:
lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: '%env(JWT_TOKEN_TTL)%'
user_identity_field: username
Then in the Symfony :
/**
* @RestPost("/api/insertUpdateSynonyms", name ="api_insert_update_synonyms")
* @RestRequestParam(name="old_cle", nullable=true)
* @RestRequestParam(name="add_synonymes", nullable=true)
* @RestRequestParam(name="new_cle", nullable=true)
* @RestRequestParam(name="new_synonymes", nullable=true)
*
* @return JsonResponse
*/
public function insertUpdateSynonyms(ParamFetcher $paramFetcher, SearchService $searchService, PublisherInterface $publisher)
{
try {
$retour = $searchService->insertUpdateSynonyms($paramFetcher);
$update = new Update(
'http://bdc.pulse.mg/notif/suggestioncontenu',
json_encode(['msg' => '============================= new content updated ! ................................'])
);
$publisher($update);
} catch (Exception $e) {
$retour = array('error' => array('error_code' => $e->getCode(), 'message' => $e->getMessage()));
}
$response = new JsonResponse();
$response->setData($retour);
return $response;
}
In the Angular side :
insertSynonyme(){
const data = {
old_cle: this.synoForm.value.cle_syno,
add_synonymes: this.dataSynonyme.oldSynonyme
};
this.generalServ.insertSynonyme(data).subscribe(
res => {
const eventSource = new EventSource('http://localhost:3000/.well-known/mercure?topic=' + encodeURIComponent('http://bdc.pulse.mg/notif/suggestioncontenu'));
eventSource.onmessage = function (event) {
// Will be called every time an update is published by the server
console.log("============== avant show data de leventsource");
console.log(JSON.parse(event.data));
}
if(res.code === 200){
this.toastr.success('Sent');
this.submitted = true;
// this._document.defaultView.location.reload();
}
});
}
At runtime the console shows nothing
Even if I replace the MERCURE_JWT_TOKEN to eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.iHLdpAEjX4BqCsHJEegxRmO-Y6sMxXwNATrQyRNt3GY
, and I start the hub with the command : mercure --jwt-key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.iHLdpAEjX4BqCsHJEegxRmO-Y6sMxXwNATrQyRNt3GY" --addr="localhost:3000" --allow-anonymous --cors-allowed-origins="*"
, then nothing is displayed in the console.
Here is hub log :
127.0.0.1 - - [09/Dec/2020:16:54:50 +0300] "GET /.well-known/mercure?topic=http%3A%2F%2Fbdc.pulse.mg%2Fnotif%2Fsuggestioncontenu HTTP/1.1" 200 8 "http://localhost:4300/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
time="2020-12-09T16:55:48+03:00" level=info msg="New subscriber" last_event_id= remote_addr="127.0.0.1:57682" subscriber_id="urn:uuid:95346ec2-44f0-40f6-b80e-8bc0a8c9649e" subscriber_topics="[http://bdc.pulse.mg/notif/suggestioncontenu]"
time="2020-12-09T16:56:43+03:00" level=info msg="Subscriber disconnected" last_event_id= remote_addr="127.0.0.1:57682" subscriber_id="urn:uuid:95346ec2-44f0-40f6-b80e-8bc0a8c9649e" subscriber_topics="[http://bdc.pulse.mg/notif/suggestioncontenu]"
127.0.0.1 - - [09/Dec/2020:16:55:48 +0300] "GET /.well-known/mercure?topic=http%3A%2F%2Fbdc.pulse.mg%2Fnotif%2Fsuggestioncontenu HTTP/1.1" 200 8 "http://localhost:4300/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
time="2020-12-09T16:56:46+03:00" level=info msg="New subscriber" last_event_id= remote_addr="127.0.0.1:57682" subscriber_id="urn:uuid:991175e2-d0e3-43b3-bb83-87ab4a475f1a" subscriber_topics="[http://bdc.pulse.mg/notif/suggestioncontenu]"
time="2020-12-09T16:57:41+03:00" level=info msg="Subscriber disconnected" last_event_id= remote_addr="127.0.0.1:57682" subscriber_id="urn:uuid:991175e2-d0e3-43b3-bb83-87ab4a475f1a" subscriber_topics="[http://bdc.pulse.mg/notif/suggestioncontenu]"
127.0.0.1 - - [09/Dec/2020:16:56:46 +0300] "GET /.well-known/mercure?topic=http%3A%2F%2Fbdc.pulse.mg%2Fnotif%2Fsuggestioncontenu HTTP/1.1" 200 8 "http://localhost:4300/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
time="2020-12-09T16:57:44+03:00" level=info msg="New subscriber" last_event_id= remote_addr="127.0.0.1:59691" subscriber_id="urn:uuid:1d18280a-2bf2-4242-b7d6-591c01abc8af" subscriber_topics="[http://bdc.pulse.mg/notif/suggestioncontenu]"
time="2020-12-09T16:58:39+03:00" level=info msg="Subscriber disconnected" last_event_id= remote_addr="127.0.0.1:59691" subscriber_id="urn:uuid:1d18280a-2bf2-4242-b7d6-591c01abc8af" subscriber_topics="[http://bdc.pulse.mg/notif/suggestioncontenu]"
So what is wrong ?
Source: Symfony4 Questions
Was this helpful?
0 / 0