Phoenix Socket Client in Swift (WIP)
Very WIP - Building a Swift client for easily connecting to the WebSocket channels on your Phoenix project. For more information on setting up Phoenix Channels check out the repo.
Working on Beta6 Compatibility
For the monent, until the Swift support in Cocoapods gets shored up, you can download the Phoenix.Socket.swift
file from the main PhoenixChat
directory of this project and include it in your application.
This relies on the Square teams SocketRocket which I added via CocoaPods.
The syntax is very similar to the Phoenix Channels syntax demonstrated on the main Phoenix Repo.
-
You will need to instantiate the Phoenix::Socket class (inside of the ViewController in my example)
let socket = Phoenix.Socket(endPoint: "ws://localhost:4000/ws")
-
You can then join the socket with the
socket.join
method as such:socket.join("YOUR CHANNEL", topic: "YOUR TOPIC", message: YOUR_MESSAGE_DICTIONARY) { channel in // do stuffs }
-
Once joined, you can handle a variety of listeners such as your initial join, new messages and new users entering the channel.
chan.on("join") { message in // say hello } chan.on("new:message") { message in // display new message } chan.on("user:entered") { message in // welcome the new user }
-
Do awesome things.