このコンテンツは選択した言語では利用できません。

Chapter 248. PubNub Component


Available as of Camel version 2.19

Camel PubNub component can be used to communicate with the PubNub data stream network for connected devices. This component uses pubnub java library.

Use cases includs:

  • Chat rooms: Sending and receiving messages
  • Locations and Connected cars: dispatching taxi cabs
  • Smart sensors: Receiving data from a sensor for data visualizations
  • Health: Monitoring heart rate from a patient’s wearable device
  • Multiplayer gamings
  • Interactive media: audience-participating voting system

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-pubnub</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

248.1. URI format

pubnub:channel[?options]
Copy to Clipboard Toggle word wrap

Where channel is the PubNub channel to publish or subscribe to.

248.2. Options

The PubNub component has no options.

The PubNub endpoint is configured using URI syntax:

pubnub:channel
Copy to Clipboard Toggle word wrap

with the following path and query parameters:

248.2.1. Path Parameters (1 parameters):

Expand
NameDescriptionDefaultType

channel

Required The channel used for subscribing/publishing events

 

String

248.2.2. Query Parameters (14 parameters):

Expand
NameDescriptionDefaultType

uuid (common)

UUID to be used as a device identifier, a default UUID is generated if not passed.

 

String

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

withPresence (consumer)

Also subscribe to related presence information

false

boolean

exceptionHandler (consumer)

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

 

ExceptionHandler

exchangePattern (consumer)

Sets the exchange pattern when the consumer creates an exchange.

 

ExchangePattern

operation (producer)

The operation to perform. PUBLISH: Default. Send a message to all subscribers of a channel. FIRE: allows the client to send a message to BLOCKS Event Handlers. These messages will go directly to any Event Handlers registered on the channel. HERENOW: Obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count. WHERENOW: Obtain information about the current list of channels to which a uuid is subscribed to. GETSTATE: Used to get key/value pairs specific to a subscriber uuid. State information is supplied as a JSON object of key/value pairs SETSTATE: Used to set key/value pairs specific to a subscriber uuid GETHISTORY: Fetches historical messages of a channel.

 

String

pubnub (advanced)

Reference to a Pubnub client in the registry.

 

PubNub

synchronous (advanced)

Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported).

false

boolean

authKey (security)

If Access Manager is utilized, client will use this authKey in all restricted requests.

 

String

cipherKey (security)

If cipher is passed, all communicatons to/from PubNub will be encrypted.

 

String

publishKey (security)

The publish key obtained from your PubNub account. Required when publishing messages.

 

String

secretKey (security)

The secret key used for message signing.

 

String

secure (security)

Use SSL for secure transmission.

true

boolean

subscribeKey (security)

The subscribe key obtained from your PubNub account. Required when subscribing to channels or listening for presence events

 

String

248.3. Message headers when subscribing

Expand
NameDescription

CamelPubNubTimeToken

The Timestamp for the event.

CamelPubNubChannel

The channel for which the message belongs.

248.4. Message body

The message body can contain any JSON serializable data, including: Objects, Arrays, Ints and Strings. Message data should not contain special Java V4 classes or functions as these will not serialize. String content can include any single-byte or multi-byte UTF-8

Object serialization when sending is done automatically. Just pass the full object as the message payload. PubNub will takes care of object serialization.

When receiving the message body utilize objects provided by the PubNub API.

248.5. Examples

248.5.1. Publishing events

Default operation when producing. The following snippet publish the event generated by PojoBean to the channel iot.

from("timer:mytimer")
    // generate some data as POJO.
    .bean(PojoBean.class)
    .to("pubnub:iot?publishKey=mypublishKey");
Copy to Clipboard Toggle word wrap

248.5.2. Fire events aka BLOCKS Event Handlers

See https://www.pubnub.com/blocks-catalog/ for all kind of serverless functions that can be invoked. Example of geolocation lookup

from("timer:geotimer")
    .process(exchange -> exchange.getIn().setBody(new Foo("bar", "TEXT")))
    .to("pubnub:eon-maps-geolocation-input?operation=fire&publishKey=mypubkey&subscribeKey=mysubkey");

from("pubnub:eon-map-geolocation-output?subscribeKey=mysubkey)
    // geolocation output will be logged here
    .log("${body}");
Copy to Clipboard Toggle word wrap

248.5.3. Subscribing to events

The following snippet listens for events on the iot channel. If you can add the option withPresens, you will also receive channel Join, Leave asf events.

from("pubnub:iot?subscribeKey=mySubscribeKey")
    .log("${body}")
    .to("mock:result");
Copy to Clipboard Toggle word wrap

248.5.4. Performing operations

herenow : Obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel

from("direct:control")
    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=herenow")
    .to("mock:result");
Copy to Clipboard Toggle word wrap

wherenow : Obtain information about the current list of channels to which a uuid is subscribed

from("direct:control")
    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=wherenow&uuid=spyonme")
    .to("mock:result");
Copy to Clipboard Toggle word wrap

setstate : Used to set key/value pairs specific to a subscriber uuid.

from("direct:control")
    .bean(StateGenerator.class)
    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=setstate&uuid=myuuid");
Copy to Clipboard Toggle word wrap

gethistory : Fetches historical messages of a channel.

from("direct:control")
    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=gethistory");
Copy to Clipboard Toggle word wrap

There is a couple of examples in test directory that shows some of the PubNub features. They require a PubNub account, from where you can obtain a publish- and subscribe key.

The example PubNubSensorExample already contains a subscribe key provided by PubNub, so this is ready to run without a account. The example illustrates the PubNub component subscribing to a infinite stream of sensor data.

248.6. See Also

  • Configuring Camel
  • Component
  • Endpoint
  • Getting Started
  • RSS
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat