> For the complete documentation index, see [llms.txt](https://rhiot.gitbook.io/rhiotdocumentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rhiot.gitbook.io/rhiotdocumentation/index-1/index/camel_pubnub_component.md).

# Camel PubNub component

Camel PubNub component can be used to communicate with the [PubNub](http://www.pubnub.com) data stream network for connected devices. This component uses [pubnub](https://www.pubnub.com/docs/java/javase/javase-sdk.html) library

## Maven dependency

Maven users should add the following dependency to their POM file:

io.rhiotcamel-pubnub${rhiot.version}

Avaliable for rhiot.version >= 0.1.1

## General URI format

```
pubnub://<pubnubEndpointType>:channel[?options]
```

The following values are currently supported as pubnubEndpointType:

* pubsub
* presence

### URI Parameters

| Option          | Default value | Description                                                                                                                                                                            |
| --------------- | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `publisherKey`  |               | The punub publisher key optained from pubnub. Mandatory for publishing events                                                                                                          |
| `subscriberKey` |               | The punub subsciber key optained from pubnub. Mandatory when subscribing to events                                                                                                     |
| `secretKey`     |               | The pubnub secret key.                                                                                                                                                                 |
| `ssl`           | true          | Use SSL transport.                                                                                                                                                                     |
| `uuid`          |               | The uuid identifying the connection. If not set it will be auto assigned                                                                                                               |
| `operation`     | `PUBLISH`     | Producer only. The operation to perform when publishing events or ad hoc querying pubnub. Valid values are `HERE_NOW`, `WHERE_NOW`, `GET_STATE`, `SET_STATE`, `GET_HISTORY`, `PUBLISH` |

Operations can be used on the producer endpoint, or as a header:

| Operation     | Description                                                                                                                                                                                                                                        |
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `PUBLISH`     | Publish a message to pubnub. The message body shold contain a instance of  `org.json.JSONObject` or `org.json.JSONArray`. Otherwise the message is expected to be a string.                                                                        |
| `HERE_NOW`    | Read presence (Who's online) information from the endpoint channel.                                                                                                                                                                                |
| `WHERE_NOW`   | Read presence information for the uuid on the endpoint. You can override that by setting the header `CamelPubNubUUID` to another uuid.                                                                                                             |
| `SET_STATE`   | Set the state by uuid. The message body should contain a instance of `org.json.JSONObject` with any state information. By default the endpoint uuid is updated, but you can override that by setting the header `CamelPubNubUUID` to another uuid. |
| `GET_STATE`   | Get the state object `org.json.JSONObject` by for the endpoint uuid. You can override that by setting the `CamelPubNubUUID` header to another uuid.                                                                                                |
| `GET_HISTORY` | Gets the message history for the endpoint channel.                                                                                                                                                                                                 |

## Consuming:

Route that consumes messages from mychannel:

```
from("pubnub://pubsub:mychannel?uuid=master&subscriberKey=mysubkey").routeId("my-route")
.to("log:default?showHeaders=true");
```

Route that listens for presence (eg. join, leave, state change) events on a channel

```
from("pubnub://presence:mychannel?subscriberKey=mysubkey").routeId("presence-route")
.to("log:default?showHeaders=true");
```

## Producing

Route the collect data and sendt it to pubnub channel mychannel:

```
from("timer:default?period=2000").routeId("device-event-route")
.bean(EventGeneratorBean.class, "getEvent()")
.convertBodyTo(JSONObject.class)
.to("pubnub://pubsub:mychannel?uuid=deviceuuid&publisherKey=mypubkey");
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://rhiot.gitbook.io/rhiotdocumentation/index-1/index/camel_pubnub_component.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
