Listener Example Using Redis

Description

Xbasic scripts can wait for Redis commands using a listener.

Add and Run a Script to Listen on Redis Commands 

The first method to be aware of is the CreateListener method, which creates a named listener on a Redis command.

The CreateListener method in this example is listening on 'brpop myEventQueue 0'. BRPOP is a Redis command that is a blocking 'Pop' on a list named myEventQueue with a timeout set to '0', which indicates that there is no timeout.

The Script starts a thread that loops until the server is closed, or until the server receives a message or 'quit'.

The background thread gets a pointer to the named listener by calling the Extension::Listener Get Method. The call to the Read method of the Listener object blocks until a message is available.

dim redis as extension::RedisClient = extension::RedisClient::CreateClient()
redis.CreateListener("myevents","brpop myEventQueue 0")

thread_create("myEventQueue_thread",<<%code%
dim redis as extension::RedisClient = extension::RedisClient::CreateClient()
dim srv as extension::Listener 
srv = extension::Listener::Get("myevents")

file.from_string("c:\data\eventlog.txt","Events"+crlf()+"===================="+crlf())
while .not. srv.Closed() 
	dim event as extension::ListenerEvent 
	event = srv.Read()
	file.append("c:\data\eventlog.txt",event.data+crlf())
	if event.data = "quit" then
		exit while
	end if
end while
%code%)

Interacting with the List the Thread is running on 

After adding and running the script defined above, change to the interactive window and check that the thread is running.

Check the contents of the log file we created in the thread using file.to_string().

? thread_enum()
= Main
myEventQueue_thread


? file.to_string("c:\data\eventlog.txt")
= Events
====================

Push an event on the list, the message should appear in out log file.

dim redis as extension::RedisClient = extension::RedisClient::CreateClient()
redis.LPush("myEventQueue","Hello world")

? file.to_string("c:\data\eventlog.txt")
= Events
====================
Hello world


redis.LPush("myEventQueue","Another Message")
? file.to_string("c:\data\eventlog.txt")
= Events
====================
Hello world
Another Message

Push an event called 'quit'' the list, which should cause the thread to stop running.

redis.LPush("myEventQueue","quit")
? thread_enum()
= Main