Reference
Reference the Hop Go Leap client in your imports. This is a package inside ofhop-go
, our Go SDK:
GO
Creating a Leap Instance
To use the client, you need to create a token in your project. This is different to a regular project token and is Leap specific. This can be done either programatically or on the Leap dashboard. When you have this, you can make the Leap client:GO
Listening for Message Events
To make a channel for message events we will want to use theMessageEventChannel
method and then loop around the channel. It is very
important with these channels that you listen for the ok signal since the
channel might be closed if the client is not reconnecting after an error or if
it was closed.
When we use the channels, we will want to create it once and then use it. By
nature of how it is implemented, if you need the events in 2 different places,
you can call this function in multiple goroutines and get seperate channels that
return the same content:
GO
These channels will leak if you make them for every usage of them since like
all Go channels, they are designed to be closed on the sender side. If you
need to send a channels contents to many disposable items, it is suggested
that you go ahead and have one goroutine that does this dispatching.
Data
field on the event contains a
map with the JSON data in, the EventName
field contains the event name, and
ChannelID
contains the channel ID or is blank if this is a direct message.
Listening for Channel Events
To make a channel for events relating to channels we will want to use theChannelEventChannel
method and then loop around the channel. It is very
important with these channels that you listen for the ok signal since the
channel might be closed if the client is not reconnecting after an error or if
it was closed.
When we use the channels, we will want to create it once and then use it. By
nature of how it is implemented, if you need the events in 2 different places,
you can call this function in multiple goroutines and get seperate channels that
return the same content:
GO
These channels will leak if you make them for every usage of them since like
all Go channels, they are designed to be closed on the sender side. If you
need to send a channels contents to many disposable items, it is suggested
that you go ahead and have one goroutine that does this dispatching.
any
. The reason for this is that channel events can be a variety of different
things. We will want to switch on the type:
GO
Listening for State Updates
Due to the asynchronous nature of this library and the fact the internet is a spooky place, we might need to asynchronously make some decisions to try and regain the connection. Whilst this happens in the background for the most part, it is likely desirable for you to track this within your application. This is especially useful for errors where the client will not backoff since you will want to kill instances of the connection. To do this, we will want to use theAddStateUpdateListener
function. This
takes a function which we can use to watch the state:
GO
Connecting
To make the connection to Leap, we will want to call theConnect
method:
GO
It is important to note that a fair amount of the authentication within the
Leap SDK is done asynchronously. Therefore, whilst this will give you a good
idea if anything with the initial upgrade/packet sending has failed, it will
not give you more information than that. Therefore, it is important you listen
for state updates.
Logging
Whilst the Leap client has a fmt logger built in that you can use, it is likely desired in production that you implement the logging interface yourself. The SDK tries to be very unopinionated here since different logging implementations will want to manage this differently. To implement our own logging client, we will go ahead and make a struct:GO
GO
err
param. This
param can be nil, so this is a consideration you should keep in mind when you
are logging:
GO
leap.NewFmtLogger()
. The
logging level is controlled by the HOP_LOGGING_LEVEL
environment variable. The
default is info
. The levels are debug
, info
, warn
, and error
. To
prevent unexpected behaviour, if the logger is nil, a NopLogger will be made and
nothing will be logged.
Subscribing to Channels
If you wish to subscribe to a channel, you can use the Subscribe function. A waiter will be added to the dispatcher, and the result will be sent back to you as either a channel partial or an error:GO