elixir-sippet icon indicating copy to clipboard operation
elixir-sippet copied to clipboard

Improve explanation for the Sippet.send function in README.md

Open martinos opened this issue 4 years ago • 5 comments

I am trying migrate my app to the latest version of Sippet and I don't understand how to send a response back to the caller.

When I read the README file, I see

Sippet.send(:sippet, request)

I don't understand what is the :sippet atom. Should it be the stack name?

Prior to the migration my Core module add a function as such:

  def receive_request(request, server_key) do
    request
    |> except_to_500(&Auth.auth(&1))
    |> Transactions.send_response(server_key)
  end

I am wondering how can I migrate this to the latest version of the library.

martinos avatar Sep 21 '21 01:09 martinos

Hi @martinos in the new version :sippet in the examples is the stack name. In this new version each stack has a name that you give at your app start:

Sippet.start_link(name: :mystack)  # could be :sippet

I believe you will want to start it at your supervision tree, so just add it as:

children = [
  {Sippet, name: :mystack}
]

Don't forget to start the transport layer Sippet.Transports.UDP.start_link(name: :mystack). It can be added to a supervision tree this way:

children = [
  {Sippet, name: :mystack},
  {Sippet.Transports.UDP, name: :mystack}
]

And like so you just call Sippet.send/2 passing first argument as the stack name and the second as the message you want to dispatch, be it a request or a response. The stack will figure out how to route it through the stack (passing to server or client transaction etc).

So receive_request/2 will look like:

  def receive_request(request, _server_key) do
    response =
      request
      |> except_to_500(&Auth.auth(&1))
    Sippet.send(:sippet, response)
  end

In long living dialogs, the client_key and server_key can be used to associate independent processes that perform the actual background operations (such as accessing databases, interacting with the user, and so on), by means of a Registry.

balena avatar Sep 21 '21 03:09 balena

In your example, If understand well, when I see:

  def receive_request(request, _server_key) do
    response =
      request
      |> except_to_500(&Auth.auth(&1))
    Sippet.send(:sippet, response)
  end

I should replace the :sippet for :mystack. It should give this instead:

def receive_request(request, _server_key) do
  response =
    request
    |> except_to_500(&Auth.auth(&1))
  Sippet.send(:mystack, response)
end

I imagine that this is the case because it works great.

Is there a way to run 2 sippets listening on two different ports? I tried and it does not seem to work.

  def start(_type, _args) do
    children = [
      {Sippet, name: :as_stack},
      {Sippet.Transports.UDP, name: :as_stack, port: 5062},
      {Sippet, name: :vs_stack},
      {Sippet.Transports.UDP, name: :vs_stack, port: 5063}
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]

    res = Supervisor.start_link(children, opts)
    Sippet.register_core(:as_stack, MyApp.AsCore)
    Sippet.register_core(:vs_stack, MyApp.VsCore)
    res
end

I didn't touch Elixir for a while so I am a bit confused.

martinos avatar Sep 21 '21 14:09 martinos

Yes, your receive_request/2 function looks fine.

And yes, the purpose of the new Sippet version is to permit the creation of multiple stacks just as you mentioned.

balena avatar Sep 21 '21 23:09 balena

Thanks, With the code above I have the following error:

* (Mix) Could not start application my_app: exited in: MyApp.Application.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (ArgumentError) unknown registry: :as_stack
            (elixir 1.12.2) lib/registry.ex:1097: Registry.put_meta/3
            (my_app 0.3.0) lib/my_app/application.ex:31: MyApp.Application.start/2
            (kernel 6.5.2.1) application_master.erl:277: :application_master.start_it_old/4

The error is associated with the following line.

    Sippet.register_core(:as_stack, MyApp.AsCore)

I don't understand why I have that error since the:as_stacksippet is in the children list. If I remove the :vs_stack from the list, it works well. It seems that adding a second Sippet stack creates an error when I try to register the first one.

Do you have an idea why?

martinos avatar Sep 22 '21 01:09 martinos

@balena, I am not sure where to call the Sippet.register_core function. Can you help me on that ?

martinos avatar Nov 15 '21 15:11 martinos

Closed as no new activity has been registered. Please open a new issue if the problem persists.

balena avatar Jan 06 '24 10:01 balena