-1

sls documentation says

Websockets make it possible to add support for a bi-directional communication channel between clients and servers. Connection channels are kept alive and are re-used to exchange messages back-and-forth.

That's great.

Looking at the bref documentation however, this is not evident at all. Is it possible for the $connect handler to write data somewhere -- /tmp, APCu extension etc etc -- which later handlers could read? Without websocket the answer is "no" because that's how AWS Lambda work.

chx
  • 11,270
  • 7
  • 55
  • 129

1 Answers1

0
service: chromedriver

provider:
  name: aws
  ecr:
    images:
      chromedriver:
        path: ./

functions:
  chromedriver:
    image:
      name: chromedriver
    events:
      - websocket: $connect
      - websocket: $disconnect
      - websocket:
          route: $default
          routeResponseSelectionExpression: $default

You can see an example of the Dockerfile at https://bref.sh/docs/web-apps/docker.html , mine is the same as the example except it also adds Chrome and Chromedriver. Here's the handler PHP:

<?php

use Bref\Context\Context;
use Bref\Event\ApiGateway\WebsocketEvent;
use Bref\Event\ApiGateway\WebsocketHandler;
use Bref\Event\Http\HttpResponse;

require __DIR__ . '/vendor/autoload.php';

class Handler extends WebsocketHandler {
  public function handleWebsocket(WebsocketEvent $event, Context $context): HttpResponse {
    switch ($event->getEventType()) {
      case 'CONNECT':
        file_put_contents('/tmp/log.txt', $event->getConnectionId());
        break;
    }
    return new HttpResponse(@file_get_contents('/tmp/log.txt') ?: 'not found');
  }
}
return new Handler();

Testing this:

wscat  --connect wss://url.from.the.output.of.serverless.deploy/dev
Connected (press CTRL+C to quit)
> test
< fd4xBe7HIAMCK9A=
> test
< fd4xBe7HIAMCK9A=

Same connection ID both times. As far as I understand -- and that's not too far -- this is probably just a "warm start" of Lambda but that is sufficient for my purposes.

chx
  • 11,270
  • 7
  • 55
  • 129