Getting Started
Neffos provides a comprehensive API to work with. Identical API and keywords between server and client sides.
Here is a quick outline of the flow you'll use:

All features are always in-sync between server and client side connections, each side gets notified of mutations.
End-developers can implement deadlines to actions like Conn#Connect, Conn#Ask, NSConn#Disconnect, NSConn#Ask, NSConn#JoinRoom, Room#Leave and etc. Methods that have to do with remote side response accept a context.Context as their first argument.
Dialing
Client connection is initialized through the
neffos.Dial(orneffos.dialon javascript side) method.Server can dismiss with an error any incoming client through its
server.OnConnect -> return err != nilcallback.
Send data through events
Emitsends data and fires a specific event back to the remote side.A client can NOT communicate directly to the rest of the clients.
Server is the only one which is able to send messages to one or more clients.
To send data to all clients use the
Conn.Server().Broadcastmethod. If its first argument is not nil then it sends data to all except that one connection ID.
Now, let's start by building a small echo application between server and a client. Client will send something and server will respond with a prefix of "echo back: ".
Supposedly we have both server and client side in the same project (or package).
Create a new echo.go file.
Import the neffos go package and, optionally, the log standard go package in order to help us print incoming messages.
Register events
Incoming events are being captured through callbacks. A callback declaration is a type of func(*neffos.NSConn, neffos.Message) error.
Note that in this example, we share the same event's callback across client and server sides, depending on your app's requirements you may want to separate those events.
The neffos.Reply is just a helper function which writes back to the connection the same incoming neffos.Message with a custom body data.
Alternatively you may Emit a remote event, depending on your app's needs, this is how:
Which is the same as:
Callbacks are registered via the Namespaces & Events event-driven API. Let's add an "echo" event on a namespace called "v1". Note that Events can be registered without a namespace as well but it's highly recommented that you put them under a non-empty namespace for future maintainability.
Using a struct value
You can also register events based on a Go structure using the NewStruct function which returns a compatible neffos.ConnHandler. Like the neffos.Namespaces, neffos.Events and neffos.WithTimeout can be passed on New and Dial package-level functions.
Example Code:
Please read the comments.
Create the Client
A neffos (Go) client expects a compatible neffos.Dialer to dial the neffos websocket server.
Among the neffos import statement, add one of the two built-in compatible Upgraders & Dialers. For example the neffos/gorilla sub-package helps us to adapt the gorilla websocket implementation into neffos.
Read more about Upgraders and Dialers.
Creating a websocket client is done by the Dial package-level function.
The Dial function accepts a context.Context as its first parameter which you can use to manage dialing timeout.
The url as its third input parameter is the endpoint which our websocket server waits for incoming requests, i.e ws://localhost:8080/echo.
The final parameter you have to pass to create both client and server is a ConnHandler (Events or Namespaces with Events).
Connect the Client to a Namespace
The client must be connected to one or more server namespaces (even if a namespace is empty "") in order to initialize the communication between them.
Let's connect our client to the "v1" namespace and emit the remote (in this case, server side's) "echo" event with the data of "Greeetings!".
The client's onEcho will be fired and it will print the message of echo back: Greetings!.
Note that event emitting and firing are asynchronous operations, if you exit the program immediately after it, there is not enough time to fire back the "echo" event that the server-side remotely fired from its side.
However, if you need to block until response is received use the c.Ask instead:
In that case, the responseMessage.Body is expected to be []byte("echo back: Greetings!").
Create the Server
Creating a websocket server is done by the neffos.New package-level function.
A neffos server expects a compatible neffos.Upgrader to upgrade HTTP incoming connection to WebSocket.
Run the Server
A websocket server should run inside an http endpoint, so clients can connect. We need to create an http web server and add a route which will serve and upgrade the incoming requests using the neffos server. For that, you can choose between standard package net/http and a high-performant package like iris.
The neffos.Server completes the http.Handler interface.
Assuming that we want our websocket clients to communicate with our websocket server through the localhost:8080/echo endpoint, follow the below guide.
If you choose net/http, this is how you register the neffos server to an http endpoint:
Access the http.Request from Conn:
Otherwise install iris using the go get -u github.com/kataras/iris/v12@latest terminal command. Iris has its own adapter for neffos, it lives inside its iris/websocket sub-package, we need to import that as well.
Access the iris.Context from Conn:
Putting it all together
The final program, which contains both server and client side is just 75 lines of code.
Run it
The Javascript library is aligned with the Go's client methods.
1. Import the neffos.js library, as module:
Or in a script HTML tag:
2. For example, to register events and dial:
OR
A javascript equivalent client looks like that.
Last updated