Vlang tutorials — playing with WebSockets

Kevin Da Silva
3 min readDec 23, 2023

Hey, guys here I am back again with one more fast-paced Vlang tutorial, this time covering a bit more of the net module web socket support.

In this tutorial, I will cover the basic setup of both client and server and how to connect them through WebSockets

The server

Let’s start with setting up the server, we will have to create a new file called server.v and import net and net.websocket:

import net.websocket as socket
import net

To start up a server we need to call the new_server function from the web socket, the docs say we have to pass the following params:

fn new_server(
family net.AddrFamily,
port int,
route string,
opt ServerOpt)
&Server

So let’s create first the address family, for this example, I want to use IP4, so I will set it as ip:

addr := net.AddrFamily.ip  

for opt I will just use the default implementation:

opt := socket.ServerOpt {}

For port and route, I will use 3000 and /hi, so our new_server setup should look like this:

mut server := socket.new_server(addr, 3000, "/hi", opt)

For teaching purposes I will use only two server callbacks to show how the module works: on_connection to get when a client connects and on_message to be able to process client input

First let’s make our server able to tell us when a client connected:

server.on_connect(fn(mut _ socket.ServerClient) !bool {
println("client connection")
return true
})!

We just pass a function as a parameter and print that a client connected to our server, relatively simple, let’s make it a bit spicier and get the user input:

server.on_message(fn (mut _ socket.Client, msg &socket.Message) ! {
payload := msg.payload
if payload.len > 0 {
println(payload.bytestr())
}
})

We also use a function as a parameter to be able to get the message, notice that before printing the message I check if the payload has bytes in it or if it’s just empty, if not empty I convert it to a string because in this example we will just work with strings, but I could be working with JSON or other encoding formats.

Now we just have to call the listen method to be able to listen to the incoming connections:

server.listen()!

By that point, our server.v should contain the following contents:

import net.websocket as socket
import net

opt := socket.ServerOpt {}

addr := net.AddrFamily.ip

println(addr)

mut server := socket.new_server(addr, 3000, "/hi", opt)

server.on_connect(fn(mut _ socket.ServerClient) !bool {
println("client connection")
return true
})!

server.on_message(fn (mut _ socket.Client, msg &socket.Message) ! {
payload := msg.payload
if payload.len > 0 {
println(payload.bytestr())
}

})

server.listen()!

The client

The client is way more simple to set up than the server and we can make it work in just a few lines of code

Create a new file client.v and import net.websocket:

import net.websocket as socket

To create a client we have to use the new_client function with the following params:

fn new_client(address string, opt ClientOpt) !&Client

Let´s first set the opt that consists basically of using the default struct:

opt := socket.ClientOpt {}

And now it´s just connect the client to the server url:

mut client := socket.new_client("ws://127.0.0.1:3000/hi", opt)!

Now to connect to the server and print the message “client connection” on the server we have to call the connect method:

client.connect()!

And to send the string message to the server we have to call the write_string method

client.write_string("hi from client")!

And to be able to listen to the server we just have to call listen on the client as well:

client.listen()!

By now our client.v should look like this:

import net.websocket as socket

opt := socket.ClientOpt {}

mut client := socket.new_client("ws://127.0.0.1:3000/hi", opt)!

client.connect()!

client.write_string("hi from client")!

client.listen()!

And to run it we need to open up two terminal windows and hit the commands:

v run server.v //to start the server
v client.v // to start the client

And that’s how you make sockets work with Vlang, hope you liked it!

Happy holidays, bye :)

--

--

Kevin Da Silva

I'm a back-end developer, functional programming lover, fascinated by computer science and languages. From the south part of Brazil to the world