3

I am developing a chat that currently works with continuous server requests, with the waste of resources that results.

So I thought to avail of the services offered by the pusher platform. The problem is that I need to save each message exchanged by clients on my database. For all I know, pusher uses the server-side only for the accreditation of channels and message forwarding to clients, while the communication between clients does not pass through my server.

I've probably already found the answer to my question in this thread (moonlight response)

except that I have no idea how to implement something like that ... some help?

Community
  • 1
  • 1
ciccioassenza
  • 233
  • 1
  • 7
  • 17

2 Answers2

3

You can trigger chat message events in one of two ways:

  1. Client -> Your Server -> Pusher HTTP API -> All clients
  2. Client -> Pusher WebSocket API -> All Clients

In scenario 1 the messages are going via your own server so you can easily store them in a database.

In scenario 2 you can set up client event WebHooks. Using these the message route is:

Client -> Pusher WebSocket API -> All Clients && WebHook -> You Server

You can then store the client event messages in your database when they arrive via the WebHook.

How to set up a Pusher WebHook

You set up a WebHook for your Pusher App within the dashboard.

Simply choose WebHooks setting for the app.

WebHook setting

Create a new "client event" WebHook using an endpoint on your own application as the URL:

Create a Pusher WebHook

The WebHook will be created so any time a client event is triggered for this app that endpoint will be hit.

WebHook created

Consuming a Pusher WebHook

The format of the POST request to the URL you have defined will be:

{
  "name": "client_event",
  "channel": "name of the channel the event was published on",
  "event": "name of the event",
  "data": "data associated with the event",
  "socket_id": "socket_id of the sending socket",
  "user_id": "user_id associated with the sending socket" # Only for presence channels
}

So, you need to parse the body of the request and get the information you require.

Please be sure to read the WebHook docs and follow the security guidelines.

leggetter
  • 15,248
  • 1
  • 55
  • 61
  • Thats what I looking for. I have contacted the pusher support and I was advised to use their webhooks, so now I'm trying to figure out how to implement them (I use laravel in backend and as3 for mobile clients). Could you give me further explanation of how to implement both scenarios? I would be very grateful! Anyway your answer is very satisfactory, so I guess will mark it as the best response. Thank you sir. – ciccioassenza Mar 19 '15 at 09:39
  • I've added details on setting up the WebHook and basic details on the payload. The Laravel specific should be pretty easy from there. – leggetter Mar 19 '15 at 15:31
0

Have you considered using a cloud database with data-sync like Firebase or Realtime Storage? You would get the durable message persistence and real-time communication when data is updated/inserted in a single API.

  • 1
    Thanks for the reply. Yes I've considered Realtime Storage, but when I say that I'm developing a chat, i mean I'm developing a client-server application where one functionality is a chat (group and private). In my backend I do mutch other stuff, so I need my server is the in the middle of all comunication. – ciccioassenza Mar 18 '15 at 19:31