Start subscribing with C
Create a C client within minutes that connects to the Diffusion™ server. This example creates a client that subscribes to a JSON topic called "processes" and prints its value to the console when the topic is updated.
For more information about dependencies on each supported platform, see C.
- By including diffusion.h before any APR includes. The diffusion.h file sets these values.
- As command-line flags
To complete this example, you need a Diffusion server.
- create a named user that has a role with the and permissions
- assign a role with those permissions to anonymous client sessions.
The client prints to the console every time the value of the subscribed topic is updated. You can update the value of the topic by creating a publishing client to update the topic. By default, the client subscribes to the processes topic used in the Start publishing with C example.
# The following two variables must be set. # # Directory containing the C client include files. # DIFFUSION_C_CLIENT_INCDIR = # # Directory containing libdiffusion.a # DIFFUSION_C_CLIENT_LIBDIR = ifndef DIFFUSION_C_CLIENT_INCDIR $(error DIFFUSION_C_CLIENT_INCDIR is not set) endif ifndef DIFFUSION_C_CLIENT_LIBDIR $(error DIFFUSION_C_CLIENT_LIBDIR is not set) endif CC = gcc # Extra definitions from parent directory, if they exist. -include ../makefile.defs CFLAGS += -g -Wall -Werror -std=c99 -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=700 -I$(DIFFUSION_C_CLIENT_INCDIR) LDFLAGS += $(DIFFUSION_C_CLIENT_LIBDIR)/libdiffusion.a -lpthread -lpcre -lz $(LIBS) # If you have openssl installed then you can uncomment these. ifdef HAVE_OPEN_SSL LDFLAGS += -lssl -lcrypto endif ARFLAGS += SOURCES = json/cjson-subscribing-example.c TARGETDIR = target OBJDIR = $(TARGETDIR)/objs BINDIR = $(TARGETDIR)/bin OBJECTS = $(SOURCES:.c=.o) TARGETS = cjson-subscribing-example all: prepare $(TARGETS) .PHONY: all prepare: mkdir -p $(OBJDIR) $(BINDIR) $(OBJDIR)/%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< cjson-subscribing-example: json/cjson-subscribing-example.c $(CC) $^ $(CFLAGS) $(LDFLAGS) -lm -o $(BINDIR)/$@ clean: rm -rf $(TARGETS) $(OBJECTS) $(TARGETDIR) core a.out
This page last modified: 2017/08/11