A slim implementation of a websocket server using Swift and Vapor 4.0.

Overview

Swift Websocket Server Example using Vapor 4.0

This project includes a minimum working example for a websocket server written in Swift. To interact with it I recommend using websocat (Github-Link).

Part of the challenge was to binary-encode and decode JSON payload into predefined Swift structs which are stored in the Sources/App/websocket/messages folder.

How to use

  1. Build and run using swift build and swift run
  2. In this example the websocket is served under the /channel path. In the terminal connect to the websocket server using websocat:
    # '-b' transfers payload binary encoded
    websocat -b ws://127.0.0.1:8080/channel
  3. Paste into the console the JSON payload in the valid format (it is of type WebsocketMessage<Connect>):
    {"client":"C13C2DA8-13FA-4BA6-A361-61488AC5B66A","data":{"connect":true}}
  4. The websocket server should return the following message which should be displayed in the terminal decoded as string:
    {"client":"C13C2DA8-13FA-4BA6-A361-61488AC5B66A","data":{"name":"Adrian","male":true,"age":33}}
You might also like...
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)

Perfect: Server-Side Swift 简体中文 Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

A Swift web framework and HTTP server.
A Swift web framework and HTTP server.

A Swift Web Framework and HTTP Server Summary Kitura is a web framework and web server that is created for web services written in Swift. For more inf

Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.

Why Zewo? • Support • Community • Contributing Zewo Zewo is a lightweight library for web applications in Swift. What sets Zewo apart? Zewo is not a w

A simple GCD based HTTP client and server, written in 'pure' Swift
A simple GCD based HTTP client and server, written in 'pure' Swift

SwiftyHTTP Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift

Swift Express is a simple, yet unopinionated web application server written in Swift
Swift Express is a simple, yet unopinionated web application server written in Swift

Documentation h5 align="right"a href="http://demo.swiftexpress.io/"Live 🐧 server running Demo img src="https://cdn0.iconfinder.com/data/icons/

Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

🔌  Non-blocking TCP socket layer, with event-driven server and client.
🔌 Non-blocking TCP socket layer, with event-driven server and client.

Original authors Honza Dvorsky - http://honzadvorsky.com, @czechboy0 Matthias Kreileder - @matthiaskr1 At the request of the original authors, we ask

A place to share code and server configurations in support of the networkQuality tool

Network Quality Server Welcome! The Network Quality Server project was created as a place to share example servers that can used by the networkQuality

A tool to build projects on MacOS and a remote linux server with one command

DualBuild DualBuild is a command line tool for building projects on MacOS and a remote Linux server. ##Setup Install the repository git clone https://

Comments
  • Best example so far.

    Best example so far.

    Hi, and thanks for creating the best example Iv'e been able to find. It compiles and runs on macOS, but unfortunatley not on Linux. On the iMac, and using an app called Socket Debugger, sending {"client":"C13C2DA8-13FA-4BA6-A361-61488AC5B66A","data":{"connect":true}} only returns a pong. I'm hoping you'll consider Linux and and implementing WSS secure sockets. Looking through the code, I can't find a way to customise the host address and port, because on Linux I would need it to run on 0.0,0,0 and a port of my choice. Do keep up the good work.

    opened by ea7kir 2
Owner
Adrian Hupka
Adrian Hupka
Enable WebSocket in OPC DA/AE Server with JSON return, first time ever

WebSocket4OPC Enable WebSocket in OPC DA/AE Server with JSON return, first time ever DCOM was developed more than 2 decades ago, wich was the pillar o

null 13 Dec 14, 2022
Cross-platform JsonRPC client implementation with HTTP and WebSocket support

JsonRPC.swift Cross-platform JsonRPC client implementation with HTTP and WebSocket support Getting started Installation Package Manager Add the follow

Tesseract 5 Oct 19, 2022
Simple iOS app in Swift to show AQI for some cities using websocket using Combine + MVVM

AQI Simple iOS app in Swift to show AQI for some cities using websocket using Combine + MVVM This app follows MVVM This app uses combine framework The

Amey Vikkram Tiwari 2 Nov 6, 2022
WebSocket(RFC-6455) library written using Swift

DNWebSocket Object-Oriented, Swift-style WebSocket Library (RFC 6455) for Swift-compatible Platforms. Tests Installation Requirements Usage Tests Conf

Gleb Radchenko 36 Jan 29, 2022
Conforming WebSocket (RFC 6455) client library for iOS and Mac OSX

SwiftWebSocket Conforming WebSocket (RFC 6455) client library for iOS and Mac OS

null 0 Dec 24, 2021
OBSwiftSocket is a Swift library to be used for communication with OBS Studio via obs-websocket (v5).

OBSwiftSocket OBSwiftSocket is a Swift library to be used for communication with OBS Studio via obs-websocket (v5). obs-websocket v5 specification: ht

Edon 3 Sep 28, 2022
A conforming Objective-C WebSocket client library.

SocketRocket A conforming WebSocket (RFC 6455) client library for iOS, macOS and tvOS. Test results for SocketRocket here. You can compare to what mod

Facebook Incubator 9.4k Dec 27, 2022
Swift package for adding API Key requirement to vapor backends.

APIKeyMiddleware Swift package for adding API Key requirement to vapor backends. Features ✅ Supports an array of keys ✅ Supports overriding thrown err

null 2 Dec 9, 2021
Swift HTTP server using the pre-fork worker model

Curassow Curassow is a Swift Nest HTTP Server. It uses the pre-fork worker model and it's similar to Python's Gunicorn and Ruby's Unicorn. It exposes

Kyle Fuller Archive 397 Oct 30, 2022
FlyingFox - a lightweight HTTP server built using Swift Concurrency

Usage Credits Introduction FlyingFox is a lightweight HTTP server built using Swift Concurrency. The server uses non blocking BSD sockets, handling ea

Simon Whitty 262 Dec 24, 2022