Latest 0.6.0
License MIT
Platforms ios 9.0
Dependencies Starscream

PhoenixWebSocket is a websockets framework designed to work with Phoenix Framework. It uses Starscream under the hood.



github "almassapargali/PhoenixWebSocket"



Don’t forget to append /websocket, to specify transport, to you socket path.

let url = NSURL(string: "ws://localhost:4000/socket/websocket")!
// create socket and channel
socket = Socket(url: url)

channel = Channel(topic: "rooms:lobby")

// you can optionally set join payload
channel.joinPayload = ["user_id": 123]


// you can optionally enable logging
socket.enableLogging = true

// then connect

This framework users NSTimers extensively for reconnecting or sending heartbeat message. And since iOS invalidates all NSTimers when app goes background, it’s recommended to call connect again on UIApplicationWillEnterForegroundNotification. Like:

override func viewDidLoad() {

  subscription = NSNotificationCenter.defaultCenter().addObserverForName(
    UIApplicationWillEnterForegroundNotification, object: nil, queue: nil) { [weak self] _ in

deinit {
  if let observer = subscription {

And, also, since NSTimers retains their targets, it’s hardly recommended to call disconnect on deinit. Disconnecting would invalidate all timers:

deinit {

Token authentication

When creating socket:

let params = ["token": "abc...", ...]
socket = Socket(url: url, params: params)

And in server:

def connect(%{"token" => token}, socket) do

Receiving messages

  // channel connection status
  .onStatusChange { newStatus in ... }
  .on("new:msg") { message in print(message.payload) }
  .on("user:joined") { message in ... }

Sending messages

You can optionally pass message callback if server replies to this message.

let payload = ["user": "Chuck Norris", "body": "Two seconds till"]
socket.send(channel, event: "new:msg", payload: payload) { res in
  switch res {
    case .Success(let response): // received server response
      switch response { // server replied on handle_in with {:reply, response, socket}
      case .Ok(let payload): // response is {:ok, payload}
      case let .Error(reason, payload): // response is {:error, %{reason: "Good reason"}}
    case .Error(let error): // connection error

See demo app

To run demo app:

  1. Clone chrismccord/phoenix_chat_example and run it locally.
  2. Clone this repo, then init and update submodules.
  3. Open PhoenixChatDemo/PhoenixChatDemo.xcodeproj and run it.


PhoenixWebSocket is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "PhoenixWebSocket",
    "version": "0.6.0",
    "license": "MIT",
    "summary": "A websockets framework designed to work with Phoenix Framework",
    "homepage": "",
    "authors": "Almas Sapargali",
    "source": {
        "git": "",
        "tag": "0.6.0"
    "source_files": "PhoenixWebSocket/*",
    "platforms": {
        "ios": "9.0"
    "dependencies": {
        "Starscream": [
            "~> 2.0"
    "pushed_with_swift_version": "3.0"

Pin It on Pinterest

Share This