Start subscribing with C
Create a C client within minutes that connects to Diffusion™ Cloud. This example creates a client that prints the value of a topic 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 Cloud service. For more information about getting a Diffusion Cloud service, see Getting started with Diffusion Cloud.
You also require either a named user that has a role with the "CLIENT" role. For more information about roles and permissions, see Role-based authorization.
and permissions or that anonymous client connections are assigned a role with the and permissions. For example, theThe client prints a the value to the console every time the value of the foo/counter topic is updated. You can update the value of the foo/counter topic by using the Diffusion Cloud Dashboard's test client or by creating a publishing client to update the topic. To create and publish to the foo/counter topic, you require a user with the and permissions. For more information, see Start publishing with C.
#include <stdio.h>
#include <unistd.h>
#include "diffusion.h"
#include "args.h"
/*
* When a subscribed message is received, this callback is invoked.
*/
static int
on_topic_message(SESSION_T *session, const TOPIC_MESSAGE_T *msg)
{
printf("%.*s\n", (int)msg->payload->len, msg->payload->data);
return HANDLER_SUCCESS;
}
/*
* This callback is fired when Diffusion responds to say that a topic
* subscription request has been received and processed.
*/
static int
on_subscribe(SESSION_T *session, void *context_data)
{
printf("Subscribed to topic\n");
return HANDLER_SUCCESS;
}
int
main(int argc, char **argv)
{
/*
* Create a session
*/
DIFFUSION_ERROR_T error = { 0 };
SESSION_T *session = NULL;
// Edit this line to include the URL of your Diffusion Cloud service
session = session_create("ws://service-name.diffusion.cloud:80", "user", credentials_create_password("password"), NULL, NULL, &error);
if(session == NULL) {
fprintf(stderr, "TEST: Failed to create session\n");
fprintf(stderr, "ERR : %s\n", error.message);
return EXIT_FAILURE;
} else {
fprintf(stdout, "Connected\n");
}
subscribe(session, (SUBSCRIPTION_PARAMS_T) { .topic_selector = ">foo/counter", .on_topic_message = on_topic_message, .on_subscribe = on_subscribe });
/*
* Receive messages for 5 minutes.
*/
sleep(300);
session_close(session, NULL);
session_free(session);
return EXIT_SUCCESS;
}
# The following two variables must be set. # # Directory containing the C client include files. DIFFUSION_C_CLIENT_INCDIR = ../path-to-client/include # # Directory containing libdiffusion.a DIFFUSION_C_CLIENT_LIBDIR = ../path-to-client/lib 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 -c -I$(DIFFUSION_C_CLIENT_INCDIR) LDFLAGS += $(LIBS) $(DIFFUSION_C_CLIENT_LIBDIR)/libdiffusion.a -lpthread -lpcre -lssl -lcrypto ARFLAGS += SOURCES = getting-started.c TARGETDIR = target OBJDIR = $(TARGETDIR)/objs BINDIR = $(TARGETDIR)/bin OBJECTS = $(SOURCES:.c=.o) TARGETS = getting-started all: prepare $(TARGETS) .PHONY: all prepare: mkdir -p $(OBJDIR) $(BINDIR) $(OBJDIR)/%.o: %.c $(CC) $(CFLAGS) -o $@ $< getting-started: $(OBJDIR)/getting-started.o $(CC) $< $(LDFLAGS) -o $(BINDIR)/$@ clean: rm -rf $(TARGETS) $(OBJECTS) $(TARGETDIR) core a.out
This page last modified: 2015/05/01