Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_sse.c

SSE (Server-Sent Events) client example using Nilorea network module.

SSE (Server-Sent Events) client example using Nilorea network module

Author
Castagnier Mickael
Version
1.0
Date
26/03/2026
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_str.h"
#include "nilorea/n_log.h"
#include <getopt.h>
#include <string.h>
#define MAX_EVENTS 3
static int event_count = 0;
static N_SSE_CONN* g_conn = NULL;
void usage(void) {
fprintf(stderr,
" -V 'log level' : set the log level (LOG_NULL, LOG_NOTICE, LOG_INFO, LOG_ERR, LOG_DEBUG)\n"
" -h : help\n");
}
#ifdef HAVE_OPENSSL
static void on_sse_event(N_SSE_EVENT* event, N_SSE_CONN* conn, void* user_data) {
(void)user_data;
g_conn = conn;
fprintf(stdout, "SSE event %d: type=%s data=%s id=%s\n",
(event->event && event->event->data) ? event->event->data : "(default)",
(event->data && event->data->data) ? event->data->data : "(empty)",
(event->id && event->id->data) ? event->id->data : "(none)");
n_sse_stop(conn);
}
}
#endif
int main(int argc, char* argv[]) {
int getoptret = 0;
while ((getoptret = getopt(argc, argv, "hV:")) != EOF) {
switch (getoptret) {
case 'V':
if (!strncmp("LOG_NULL", optarg, 8)) {
} else if (!strncmp("LOG_NOTICE", optarg, 10)) {
} else if (!strncmp("LOG_INFO", optarg, 8)) {
} else if (!strncmp("LOG_ERR", optarg, 7)) {
} else if (!strncmp("LOG_DEBUG", optarg, 9)) {
} else {
fprintf(stderr, "%s is not a valid log level.\n", optarg);
exit(1);
}
break;
case 'h':
default:
usage();
exit(1);
}
}
#ifdef HAVE_OPENSSL
n_log(LOG_INFO, "Connecting to SSE endpoint https://sse.dev/test ...");
g_conn = n_sse_connect("sse.dev", "443", "/test", 1, NULL, on_sse_event, NULL);
if (!g_conn) {
fprintf(stdout, "SSE server unreachable, skipping\n");
exit(0);
}
fprintf(stdout, "SSE example completed, received %d events\n", event_count);
n_log(LOG_INFO, "SSE example completed successfully");
#else
(void)log_level;
fprintf(stdout, "OpenSSL not available, skipping SSE test\n");
#endif
exit(0);
}
static void usage(void)
int main(void)
int getoptret
Definition ex_fluid.c:59
int log_level
Definition ex_fluid.c:60
static N_SSE_CONN * g_conn
pointer to connection for callback to stop
static int event_count
counter for received events
static void on_sse_event(N_SSE_EVENT *event, N_SSE_CONN *conn, void *user_data)
SSE event callback.
#define MAX_EVENTS
maximum number of events to receive before stopping
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
char * data
the string
Definition n_str.h:63
N_STR * event
event type (or NULL for default)
Definition n_network.h:1003
void n_sse_stop(N_SSE_CONN *conn)
Signal the SSE connection to stop reading.
Definition n_network.c:6617
void n_sse_conn_free(N_SSE_CONN **conn)
Free an SSE connection structure.
Definition n_network.c:6626
N_SSE_CONN * n_sse_connect(const char *host, const char *port, const char *path, int use_ssl, const char *user_agent, void(*on_event)(N_SSE_EVENT *, N_SSE_CONN *, void *), void *user_data)
Connect to an SSE endpoint and start reading events.
Definition n_network.c:6711
SSE connection handle.
Definition n_network.h:1012
SSE event received from server.
Definition n_network.h:1002
Generic log system.
Network Engine.
N_STR and string function declaration.