Skip to main content

Publishing over TCP Bridge

This section explains how devices can use the TCP bridge to communicate with Omnicore. For general information about UDP and TCP, see Protocols.

Be sure to refer to the API documentation for full details about each method described in this section. See also the TCP-related samples.

To publish over the TCP bridge:

  1. Install an TCP client on your device.

  2. Initiate a TCP connection over hostprefix.tcp.korewireless.com or a long-term support domain.

  3. Publish telemetry events.

TCP server

Omnicore supports the TCP protocol by running a managed broker that listens to the port hostprefix.tcp.korewireless.com: port. port is available in the device connection info which is reserved with IANA for secure TCP connections.

note

The TCP standard is defined for implementing a full publish/subscribe broker. However, the managed TCP bridge run by Omnicore does not support all publish/subscribe operations, such as creating arbitrary topics that devices can use to send messages between them. (Filtering can be accomplished with downstream processes running on Cloud Pub/Sub.) Omnicore uses a predefined set of topics and specific topic formats.

Publishing telemetry events

After the device is configured with an TCP client and connected to the TCP bridge, it can publish a telemetry event by issuing a PUBLISH message to an TCP topic in the following format:

path
 /REGISTRY_ID/DEVICE_ID/events

The device ID is the string ID of the device specified in the TCP client ID. The device ID is case sensitive.

Messages published to this TCP topic are forwarded to the corresponding registry's default telemetry topic. If no default Pub/Sub topic exists, published telemetry data will be lost. To publish messages to other Cloud Pub/Sub topics, see Publishing telemetry events to additional Cloud Pub/Sub topics.

The forwarded message data field contains a copy of the message published by the device, and the following message attributes are added to each message in the Cloud Pub/Sub topic:

AtributeDescription
deviceIdThe user-defined string identifier for the device, for example, thing1. The device ID must be unique within the registry.
deviceNumIdThe server-generated numeric ID of the device. When you create a device, Omnicore automatically generates the device numeric ID; it's globally unique and not editable.
deviceRegistryIdThe user-defined string identifier for the device registry, for example, registry1.
subscriptionIdThe string ID of the subscription that owns the registry and device.

The following sample shows how to send PUBLISH messages through the TCP connection:

PUBLISH messages through the TCP connection
package main

import (
"fmt"
"net"
"log"
"time"
)

func main() {
host := "hostprefix.tcp.korewireless.com"
port := "tcp_device_port"

// Construct TCP address
address := fmt.Sprintf("%s:%s", host, port)

// Connect to the TCP server
log.Printf("[main] Connecting to %s", address)
conn, err := net.Dial("tcp", address)
if err != nil {
log.Fatalf("[main] Failed to connect: %v", err)
}
defer conn.Close()

// Send data
message := "Hello, TCP device!"
log.Printf("[main] Sending message: %s", message)
_, err = conn.Write([]byte(message))
if err != nil {
log.Fatalf("[main] Failed to send message: %v", err)
}

// Receive response
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
log.Fatalf("[main] Failed to read response: %v", err)
}
response := string(buffer[:n])
log.Printf("[main] Received response: %s", response)

// Close connection
log.Printf("[main] Closing connection")
conn.Close()

log.Println("[main] Done")
}