Help with Function that returns error

I am writing a function to access document streaming in Fauna DB based on this code
This is the script to access the function

<script>
    var room = 'waiting'

    async function startStream() {
        const url = '/.netlify/functions/database-streams';
        room = 'function called'

        try {
            const response = await fetch(url);
            console.log('gr', response);
            const data = await response.json();
            room = data
        } catch (err) {
            console.log('error', err);
        }
    }
    startStream()
</script>

an this is the content of *dccument-stream.js"

const faunadb = require("faunadb");
const query = faunadb.query;
const collection_name = 'rooms';

var client = new faunadb.Client({
    secret: process.env.FAUNADB_SECRET_TEST_ROOMS,
    domain: 'db.fauna.com',
})

exports.handler = async (event) => {
    var docRef = query.Ref(query.Collection(collection_name), '2')

    var stream
    const startStream = () => {
        stream = client.stream.document(docRef)
            .on('snapshot', snapshot => {
                return {
                    statusCode: 200,
                    body: JSON.stringify(snapshot),
                }
            })
            .on('version', version => {
                return {
                    statusCode: 200,
                    body: JSON.stringify(version),
                }
            })
            .on('error', error => {
                stream.close()
                setTimeout(startStream, 1000)
                return {
                    statusCode: 200,
                    body: JSON.stringify(error),
                }
        })
        .start()
        }
        startStream()
    };

The response I get is

Response {type: “basic”, url: “http://localhost:8888/.netlify/functions/database-streams”, redirected: false, status: 500,

Can someone please show me what might be wrong?

What does the error in the Functions’ console say? Could you share the site name?

The site is

https://fauna-push-test.netlify.app

And the repo can be found at

bitbucket.org/psionman/fauna-push/

Hey there, @psionman :wave:

Thanks for following up. Echoing @hrishikesh, what does the error in the Functions’ console say? This will be beneficial for us to further understand the problem.

Additionally, I see you shared the site above. Thanks! What are the reproduction steps so that we can see what you are seeing? Please let us know!

Thanks!

Hi

Not sure what you mean by Functions’ console nor reproduction steps

This is my output in console

gr Response
App.svelte:20 error SyntaxError: Unexpected token l in JSON at position 0
bundle.css:1          Failed to load resource: the server responded with a status of 404 (Not Found)

and this in terminal

LOGS 

[10:10:16] 200 ─ 6.61ms ─ /

┌─────────────────────────────────────────────────┐
│                                                 │
│   ◈ Server now ready on http://localhost:8888   │
│                                                 │
└─────────────────────────────────────────────────┘

[10:10:17] 200 ─ 1.32ms ─ /
[10:10:17] 404 ─ 0.91ms ─ /build/bundle.css
[10:10:17] 200 ─ 5.35ms ─ /global.css
[10:10:17] 200 ─ 6.62ms ─ /build/bundle.js
Request from ::1: GET /.netlify/functions/database-streams
◈ lambda response was undefined. check your function code again
Response with status 500 in 171 ms.
[10:10:22] 200 ─ 4.06ms ─ /global.css
[10:10:22] 404 ─ 0.50ms ─ /build/bundle.css
[10:10:23] 200 ─ 4.86ms ─ /build/bundle.js.map

in terminal I run

netlify dev

This is my deploy log

Is this the port you have configured netlify dev to run on instead of the default 8888? Or is this the port the functions server is listening on? Or is this the port LiveReload is running on?

Sorry. Something else was running on 8888

I’ve revised my previous repsonse

Looking at the database-streams function in the repository you linked above, it has no return.

While the function contains code such as

.on('version', version => {
  return {
    statusCode: 200,
    body: JSON.stringify(version),
  }
})

this is only the return for startStream, not for the function as a whole. You code needs to look something like this fetch example from the Functions Playground.

What what I have read about Fauna DB streams (and please correct me if I’m wrong as I haven’t spent much time using Fauna) is it opens, and keeps open, a connection. This is going to last 10 seconds only as that is as long as serverless function will run (with a maximum of 26 seconds when upgraded.)

… This is going to last 10 seconds only as that is as long as serverless function will run (with a maximum of 26 seconds when upgraded.)

OK. So what is the recommended way to handle notifications on Netlify?

I’m not sure if there is a recommended way. I’ve not had need to do such thing myself.

The streaming example provided by Fauna is client-side, not server-side.

My use-case is that I want to notify the user when a change is made to the database. Fauna provides this on the client side as you say. But if Netlify only holds the function open for 10 secs that is a problem. Any suggestions for a way forward?

You could implement the client-side notification as per the Fauna example which does not require the use of a function at all.