Skip to content

Commit 30956a2

Browse files
committed
Added notes to readme about handling errors
1 parent 25c8f3e commit 30956a2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ chan.trigger("client-test", data: ["test": "some value"])
4343
* [Binding to events](#binding-to-events)
4444
* [Globally](#global-events)
4545
* [Per-channel](#per-channel-events)
46+
* [Receiving errors](#receiving-errors)
4647
* [Presence channel specifics](#presence-channel-specifics)
4748
* [Testing](#testing)
4849
* [Communication](#communication)
@@ -258,6 +259,45 @@ myChannel.bind("new-price", callback: { (data: AnyObject?) -> Void in
258259
})
259260
```
260261

262+
### Receiving errors
263+
264+
Errors are sent to the client for which they are relevant with an event name of `pusher:error`. These can be received and handled using code as follows. Obviously the specifics of how to handle them are left up to the developer but this displays the general pattern.
265+
266+
```swift
267+
pusher.bind({ (message: AnyObject?) in
268+
if let message = message as? [String: AnyObject], eventName = message["event"] as? String where eventName == "pusher:error" {
269+
if let data = message["data"] as? [String: AnyObject], errorMessage = data["message"] as? String {
270+
print("Error message: \(errorMessage)")
271+
}
272+
}
273+
})
274+
```
275+
276+
The sort of errors you might get are:
277+
278+
```bash
279+
# if attempting to subscribe to an already subscribed-to channel
280+
281+
"{\"event\":\"pusher:error\",\"data\":{\"code\":null,\"message\":\"Existing subscription to channel presence-channel\"}}"
282+
283+
# if the auth signature generated by your auth mechanism is invalid
284+
285+
"{\"event\":\"pusher:error\",\"data\":{\"code\":null,\"message\":\"Invalid signature: Expected HMAC SHA256 hex digest of 200557.5043858:presence-channel:{\\\"user_id\\\":\\\"200557.5043858\\\"}, but got 8372e1649cf5a45a2de3cd97fe11d85de80b214243e3a9e9f5cee502fa03f880\"}}"
286+
```
287+
288+
You can see that the general form they take is:
289+
290+
```bash
291+
{
292+
"event": "pusher:error",
293+
"data": {
294+
"code": null,
295+
"message": "Error message here"
296+
}
297+
}
298+
```
299+
300+
261301
### Unbind event handlers
262302

263303
You can remove previously-bound handlers from an object by using the `unbind` function. For example,

0 commit comments

Comments
 (0)