GithubHelp home page GithubHelp logo

real-time-ios's Introduction

A lightweight, testable, and scalable Swift package for all your WebSocket needs.

This package is open-sourced and built to support devices across Apple's ecosystem including iOS, iPadOS, watchOS, tvOS, and macOS.

Setup:

First, you'll need to add this package to your Xcode project. See Apple's official docs if you don't already know how to. View them here

How to use WebSocketManager

  • Create an instance of WebSocketManager. WebSocketManager is a generic class that requires a decodable type. This type is the model you want to use for message decoding.

Example of a websocket message for real-time stock information

{
  "stocks": [
    {
      "name": "Apple Inc.",
      "sym": "AAPL",
      "price": 141.66
    },
    {
      "name": "Snap Inc.",
      "sym": "SNAP",
      "price": 14.7
    },
    {
      "name": "Meta Platforms, Inc.",
      "sym": "META",
      "price": 170.16
    },
    {
      "name": "Alphabet Inc.",
      "sym": "GOOG",
      "price": 2370.79
    }
  ]
}

Your decodable model should look like:

struct WebSocketMessage: Decodable {
    var stocks: [Stock]
    
    stuct Stock: Decodable {
        var name: String
        var sym: String
        var price: Double 
    }
}

Now, with your newly created WebSocketManager, you can subscribe to the receive publisher. You will now receive values over time.

Receiving messages:

var subscriptions = Set<AnyCancellables>()
let webSocketManager = WebSocketManager()

webSocketManager
    .receive()
    .sink(receiveValue: { webSocketMessage in
        // handle your messages here
    }, receiveCompletion: { completion in
        // handle completion here
    }.store(in: &subscriptions)

real-time-ios's People

Contributors

connorprzybyla avatar

Watchers

 avatar

real-time-ios's Issues

Howdy!

What about a super-simple, how-to!

Your code looks great, would love to try it. Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.