Websocket question

Not sure if it’s ok to post this question here because it’s is not really related to any examples of in the book. It’s about a personal project inspired by the book.

I’m writing a backend for a simple ride-sharing iOS application (which I’ll develop later). I’m using Vapor to create the backend.

When a user makes a trip request to the API, I want to create a new trip and establish a websocket session between the user and a driver. The problem I’m having is how can I notify a driver that a request is in and add him to the session?

Here’s what I’ve come up with so far, although I’m not sure if it’s going to work:

When a trip request comes in, I would create a session and a trip object with the session id. When a driver visits the “Trip requests” tab in the app he would make a get request to retrieve active trip requests. When he then clicks on one of the trip requests, he would make a request with the session id of that particular trip to be added to that session.

The problems I’m seeing with the above solution is that that would make the User the Poster, and the Driver the Observer, which I’m not sure is the way to go since I want the driver to act as a poster (to send location updates so that the user can track the driver on the map in real time).

Another problem is that users would have to wait indefinitely before a driver accepts their request.

Is there a better way to notify the driver of a trip request? How can I go about achieving this?

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

You basically have three options - either have the driver have a web socket open the whole time to the server to notify them of a new trip that then triggers the code to establish a web socket between the user and driver. Or you can have the driver poll the server (not ideal). The other option is send a push notification to the driver app. Hope that helps!