dunglas / mercure

🪽 An open, easy, fast, reliable and battery-efficient solution for real-time communications
https://mercure.rocks
GNU Affero General Public License v3.0
3.94k stars 291 forks source link

Deploy mercure bundle in test server #722

Closed ManoloAMD closed 1 year ago

ManoloAMD commented 1 year ago

Hi excelent work guys,

Sorry for my english, i need help in deploy config please, i don't khown if have a bug or error in my config nginx, in my log Developer Tools i see this error:

EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

I read many issues but I did not find a solution.

Symfony 5.4.9 Ionic Angular 6.20.3 symfony/mercure-bundle 0.3.5

My template (nginx docker) server { listen 80 default_server; servername ; return 301 https://my-domain.es; }

server { listen 443 ssl default_server; server_name ${NGINX_DOMAIN_TEST}; root /var/www/api-project/public; ssl_protocols TLSv1.3; ssl_prefer_server_ciphers on; ssl_certificate /etc/ssl/certs/my-cert.es.chained.crt; ssl_certificate_key /etc/ssl/private/my-key.es.key;

location / {
    # try to serve file directly, fallback to index.php
    try_files $uri /index.php$is_args$args;
}

location /.well-known/mercure {
    proxy_pass http://mercure:3000/.well-known/mercure;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
    proxy_read_timeout 24h;
    proxy_http_version 1.1;
    proxy_set_header Connection "";

    ## Be sure to set USE_FORWARDED_HEADERS=1 to allow the hub to use those headers ##
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;

}

My .ts (ionic angular) subscribeEventSource() { const url = new URL('https://my-domain.es/.well-known/mercure');

url.searchParams.append(
  'topic',
  `https://${host}` + '/api/show/chat/' + this.user.rooms_id
);
const es = new EventSource(url, { withCredentials: true });

es.onmessage = (event: any) => {
  let message = JSON.parse(event.data);
  const user = this.user.users_id;
 // Do something ...
}

}

My controller

[Route("/show/chat/{id}", name: "chat_show")]

public function showChat( Request $request, Room $room, MessageRepository $messageRepository, SerializerInterface $serializer ): Response { $host = $this->toolsService->getHost(); $messages = $messageRepository->findBy( [ "room" => $room, ], ["created_at" => "ASC"] );

$messages = $serializer->serialize($messages, "json", [
  "groups" => ["message"],
]);

$userConnected = $this->user->getId();

$subscribe = "https://" . $host . "/api/show/chat/" . $room->getId();
$tokenJWT = $this->toolsService->getJWT($subscribe);

$response = new JsonResponse(
  [
    "messages" => $messages,
  ],
  Response::HTTP_OK
);

$response->headers->setCookie(
  Cookie::create("mercureAuthorization")
    ->withValue($tokenJWT)
    ->withExpires()
    ->withPath("/")
    ->withDomain($host)
    ->withSecure(true)
    ->withHttpOnly(true)
    ->withSameSite(Cookie::SAMESITE_NONE)
);

$response->headers->set("Content-Type", "text/event-stream");
$response->headers->set("Cache-Control", "no-cache");

return $response;

}

Run in device android - log DevTools : log

Config docker-compose mercure: docker-compose

Config docker-compose-override mercure: docker-override

Questions: Is there something you need to do to implement mercure-container on a server? Why no documentation of deploy in prod only localhost?

Thanks :)

dunglas commented 1 year ago

Can you show the body of the response you get please? If you could provide a repo containing a minimal reproducer, this would help a lot!

ManoloAMD commented 1 year ago

Ok right now I have a lot of work, I will do it soon

dunglas commented 1 year ago

Basically, it looks like you don't hit the Mercure hub but an HTML page.

ManoloAMD commented 1 year ago

sorry I'm busy right now, maybe soon I'll clone the issue.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.