Server-Sent Events|A server-sent event is when a web page automatically gets updates from a server for construct3

This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc. ///////////////////////////////////////////// construct3

conditions: onerror:When an error occurs onopen:When a connection to the server is opened onmessage:When a message is received

actions: connect:Server-Sent Events(SSE) connect to url Support:check browser support for server-sent events(SSE)

expressions: data:data callback url error:error ////////////////////////////////////////////////// server

For the example above to work, you need a server capable of sending data updates.

The server-side event stream syntax is simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending event streams. Example:

php code:

header('Content-Type: text/event-stream');

header('Cache-Control: no-cache');

$time = date('r');

echo "data: The server time is: {$time}\n\n";

Controlling the Reconnection-timeout The browser attempts to reconnect to the source roughly 3 seconds after each connection is closed. You can change that timeout by including a line beginning with "retry:", followed by the number of milliseconds to wait before trying to reconnect.

The following example attempts a reconnect after 10 seconds:

php code:

retry: 10000\n

Example:

php code:

header('Access-Control-Allow-Origin: *');

header('Content-Type: text/event-stream');

echo "retry: 10000\n";

$time = date('r');

echo "data: The server time is: {$time}\n\n";

Download

1.0.0.5 Stable (6.07 KB)

Info

Statistics

  • Download count127 total downloads
  • Latest download count 42 downloads of latest version
  • Average download count1 downloads per day average

Owners

  • 4 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Helpful!

  • This is something like launching Webhook events?

    Hi, would you be so kind as to create some usage example? the documentation is so scarce that I can't quite understand how it works.

    Thank you!

    • Yes They are similar

      The difference is:

      1) using SSE you pull updates to your Front-End.

      2) using Web-hook you pull updates to your Back-End.

      The first is simplier, easier to implement and manage. The second is more secure/reliable.

      Also, some server may implement long-polling, which will delay response until new data arrives, so it might be used instead of SSE.

      If you have a specific example, I will definitely create it

      • For example, I want Construct 3 to read the GPS coordinates at that moment and they can be displayed as variables from the outside. That calling a URL returns those values.