Getting Started Guide

Follow these 3 simple steps to send your first RPC message using KubeMQ in Kubernetes or Docker.

1

Deploy KubeMQ

To start using KubeMQ message queue, we first need to run a KubeMQ docker container either locally or on remote node.

Every deployment method requires an KubeMQ token. Please register to obtain your KubeMQ token.

docker run -d -p 8080:8080 -p 50000:50000 -p 9090:9090 -v kubemq-vol:/store -e KUBEMQ_TOKEN= "Your KubeMQ Token Here" kubemq/kubemq

kubectl apply -f https://get.kubemq.io/deploy?token="YOUR_KUBEMQ_TOKEN"

$ helm repo add kubemq-charts https://kubemq-io.github.io/charts
$ helm install --name kubemq-cluster --set token="Your KubeMQ Token Here" kubemq-charts/kubemq

version: '3.7'
services:
  kubemq:
    image: kubemq/kubemq:latest
    container_name: kubemq
    ports:
      - "8080:8080"
      - "9090:9090"
      - "50000:50000"
    environment:
      - KUBEMQ_HOST=kubemq
      - KUBEMQ_TOKEN="Your KubeMQ Token Here"
    networks:
      - backend
    volumes:
      - kubemq_vol:/store
networks:
  backend:
volumes:
  kubemq_vol:

2

Subscribe to a Channel

A receiver can subscribe to the  hello-command  channel with one of the following methods.

Run the following Kubetools command:

 kubetools rpc rec command "hello-command"

using System;

namespace RPC_Subscribe_to_a_Channel
{
    class Program
    {
        static void Main(string[] args)
        {

            var ChannelName = "testing_event_channel";
            var ClientID = "hello-world-subscriber";
            var KubeMQServerAddress = "localhost:50000";



            KubeMQ.SDK.csharp.CommandQuery.Responder responder = new KubeMQ.SDK.csharp.CommandQuery.Responder(KubeMQServerAddress);
            try
            {
                responder.SubscribeToRequests(new KubeMQ.SDK.csharp.Subscription.SubscribeRequest()
                {
                    Channel = ChannelName,
                    SubscribeType = KubeMQ.SDK.csharp.Subscription.SubscribeType.Commands,
                    ClientID = ClientID
                }, (commandReceive) => {
                    Console.WriteLine($"Command Received: Id:{commandReceive.RequestID} Channel:{commandReceive.Channel} Metadata:{commandReceive.Metadata} Body:{ KubeMQ.SDK.csharp.Tools.Converter.FromByteArray(commandReceive.Body)} ");
                    return new KubeMQ.SDK.csharp.CommandQuery.Response(commandReceive)
                    {
                        Body = new byte[0],
                        CacheHit = false,
                        Error = "None",
                        ClientID = ClientID,
                        Executed = true,
                        Metadata = string.Empty,
                        Timestamp = DateTime.UtcNow,
                    };

                }, (errorHandler) =>
                {
                    Console.WriteLine(errorHandler.Message);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("press any key to close RPC_Subscribe_to_a_Channel");
            Console.ReadLine();
        }
    }
}

The code snippet will available soon

The following Go code snippet is using KubeMQ’s Go SDK with gRPC interface:

package main

import (
   "context"
   "github.com/kubemq-io/kubemq-go"
   "log"
   "time"
)

func main() {
   ctx, cancel := context.WithCancel(context.Background())
   defer cancel()
   client, err := kubemq.NewClient(ctx,
      kubemq.WithAddress("localhost", 50000),
      kubemq.WithClientId("test-command-client-id"),
      kubemq.WithTransportType(kubemq.TransportTypeGRPC))
   if err != nil {
      log.Fatal(err)
   }
   defer client.Close()
   channel := "hello-command"
   errCh := make(chan error)
   commandsCh, err := client.SubscribeToCommands(ctx, channel, "", errCh)
   if err != nil {
      log.Fatal(err)
   }
   for {
      select {
      case err := <-errCh:
         log.Fatal(err)
         return
      case command, more := <-commandsCh:
         if !more {
            log.Println("Command Received , done")
            return
         }
         log.Printf("Command Received:nId %snChannel: %snMetadata: %snBody: %sn", command.Id, command.Channel, command.Metadata, command.Body)
         err := client.R().
            SetRequestId(command.Id).
            SetResponseTo(command.ResponseTo).
            SetExecutedAt(time.Now()).
            Send(ctx)
         if err != nil {
            log.Fatal(err)
         }
      case <-ctx.Done():
         return
      }
   }

}

The code snippet will available soon

For all other languages, check out the SDKs that KubeMQ supports on our documentation page

3

Send a Command Channel

After you have subscribed to a hello-command  channel, you can send your command to it.

kubetools queue receive "hello-world-queue"

using System;

namespace RPC_Send_a_Command_Channel
{
    class Program
    {
        static void Main(string[] args)
        {
            var ChannelName = "testing_event_channel";
            var ClientID = "hello-world-sender";
            var KubeMQServerAddress = "localhost:50000";

            var channel = new KubeMQ.SDK.csharp.CommandQuery.Channel(new KubeMQ.SDK.csharp.CommandQuery.ChannelParameters
            {
                RequestsType = KubeMQ.SDK.csharp.CommandQuery.RequestType.Command,
                Timeout = 10000,
                ChannelName = ChannelName,
                ClientID = ClientID,
                KubeMQAddress = KubeMQServerAddress
            });
            try
            {

                var result = channel.SendRequest(new KubeMQ.SDK.csharp.CommandQuery.Request
                {
                    Body = KubeMQ.SDK.csharp.Tools.Converter.ToByteArray("hello kubemq - sending a command, please reply")
                });                    
             
                if (!result.Executed)
                {
                    Console.WriteLine($"Response error:{result.Error}");
                    return;
                }
                Console.WriteLine($"Response Received:{result.RequestID} ExecutedAt:{result.Timestamp}"); 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

The code snippet will available soon

The following Go code snippet is using KubeMQ's Go SDK with gRPC interface:

package main

import (
  "context"
  "github.com/kubemq-io/kubemq-go"
  "log"
  "time"
)

func main() {
  ctx, cancel := context.WithCancel(context.Background())
  defer cancel()
  client, err := kubemq.NewClient(ctx,
    kubemq.WithAddress("localhost", 50000),
    kubemq.WithClientId("test-command-client-id"),
    kubemq.WithTransportType(kubemq.TransportTypeGRPC))
  if err != nil {
    log.Fatal(err)
  }
  defer client.Close()
  channel := "hello-world-queue"

  receiveResult, err := client.NewReceiveQueueMessagesRequest().
    SetChannel(channel).
    SetMaxNumberOfMessages(1).
    SetWaitTimeSeconds(5).
    Send(ctx)
  if err != nil {
    log.Fatal(err)
  }
  log.Printf("Received %d Messages:n", receiveResult.MessagesReceived)
  for _, msg := range receiveResult.Messages {
    log.Printf("MessageID: %s, Body: %s", msg.Id, string(msg.Body))
  }
}

The code snippet will available soon

For all other languages, check out the SDKs that KubeMQ supports on our documentation page

4

Congratulations!

You've sent your first Event message using KubeMQ!

Next steps:

Check out our SDK reference and code examples .NET, GO, Java, Python and REST.

Read our documatation

Send a Message