Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set the first query of LastEventId #1003

Open
abda11ah opened this issue Jan 31, 2025 · 2 comments
Open

How to set the first query of LastEventId #1003

abda11ah opened this issue Jan 31, 2025 · 2 comments

Comments

@abda11ah
Copy link

Hi,

I'm trying to avoid the "Subscriber disconnected" + "New subscriber" after each page change.

As I read in the docs, i need to send first a LastEventId query parameter to get one then reuse it to avoid loosing connection.

"When the request contains a Last-Event-ID HTTP header or a lastEventID query parameter the hub MUST set a Last-Event-ID header on the HTTP response"

I tried to set 0 as value of the LastEventId

 url.searchParams.append('lastEventID', '');
// or 
 url.searchParams.append('lastEventID', 0);
 const eventSourceOptions = {
                    withCredentials: true,
                    headers: {
                        'Authorization': `Bearer ${jwtToken}`,
                    }
                };

the connection is successful but the returned LastEventId is also 0 and i continuously get "Subscriber disconnected" + "New subscriber" + a new {"id":"urn:uuid:36d883aa-4015-4241-aaae-bde887c2662e","last_event_id":"0" .....

so please How to set the first LastEventId so it will return a random value instead of what i gave ?

@abda11ah
Copy link
Author

abda11ah commented Feb 1, 2025

At least is it possible to get the ":" heartbeat i receive each 30 seconds (the : of chrome eventstream), to update a timestamp "last_seen" SQL field in users table in the php side ? (But without POSTing again this event via ajax)

@abda11ah
Copy link
Author

OK, i just found how to :

After the first connection, i have to fetch it like this :

//my initial topic 
url.searchParams.append('topic', `user|${this.userId}`);

......
.......

// fetch it 
this.eventSource.onopen = (event) => {
                    console.log('Successfully connected to Mercure hub');


                    fetch(`${this.mercureUrl}/subscriptions/user|${this.userId}`)
                    .then(response => response.json())
                    .then(data => {
                        console.log("Initial lastEventID:", data.lastEventID);
                    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@abda11ah and others