Skip to the content.

🏠 Gofer Engine > Channel Workflows > Filtering

Filter Flows

Filter Flows are used to filter messages. They are used to determine if a message should be processed further or if it should be dropped. The interface FilterFlow can be defined as:

type FilterFunc = (msg: Msg, context: IMessageContext) => boolean
type FilterFlow = FilterFunc | { kind: 'filter'; filter: FilterFunc }

Refer to the Message Class (Msg) for more information on the Msg class and extrapolating data from the message to use in comparisons.

Refer to the Context Object for more information on the context object.

If the filter function returns true, then the message will be processed further. If the filter functions return false, then the message will be dropped. An easy catchy phrase to remember is “If it’s true, then let it through. If it’s false, then it will halt.”

Here is a simple example of a filter that will only allow ADT event messages to be processed further:

const filter = (msg: Msg) => msg.get('MSH-9.1') === 'ADT'

// OOP style
gofer.listen('tcp', 'localhost', 8080)
  .filter(filter)

// Config style
const channelConfig: ChannelConfig = {
  name: 'ADT Channel',
  source: {
    tcp: {
      host: 'localhost',
      port: 8080,
    },
  },
  ingestion: [{ filter }],
}

This could be refactored a little further to allow for more flexibility:

const onlyAllowEvents = (event: string[]) => (msg: Msg) =>
  event.includes(msg.get('MSH-9.1') as string)

// OOP style
gofer.listen('tcp', 'localhost', 8080)
  .filter(onlyAllowEvents(['ADT', 'ORM', 'ORU']))

// Config style
const channelConfig: ChannelConfig = {
  name: 'ADT/ORM/ORU Channel',
  source: {
    tcp: {
      host: 'localhost',
      port: 8080,
    },
  },
  ingestion: [onlyAllowEvents(['ADT', 'ORM', 'ORU'])],
}

For advanced type control, you can pass through a generic to the ChannelConfig (the first generic option) to either:

The default is 'B'. E.G. const conf: ChannelConfig<'B'> = ...

The OOP style channel builder filter method aligns with the following types:

IngestionClass.filter = (filter: FilterFlow<'F'>) => IngestionClass
RouteClass.filter = (filter: FilterFlow<'F>) => RouteClass

This means that the filter method accepts a single argument which is a function.