1

1.) How can I notifiy a client about an event like "you just received a message" in PHP and jquery? (maybe in the look and feel of stackoverflow)

I don't send the message itself because it is stored in a database.

Read more to understand the background of my question...

The environment:

I've build a very simple messaging system. For the implementation I used PHP and jQuery. Here is what you can do with this little messaging system:

Client:

  1. Type and send a message to desired contact.
  2. Click on a contact to read whole conversation (from database).

Server:

  1. Receive a message and store it in a database
  2. MISSING: Notify client when a message is stored for him

This is all you need to know I guess.

As a client I need to reload the page manually to get the message content

Today I've read a lot about websockets or the possibility to send a notification from server to client via SSE and ajax.

Thoughts about Websockets:

There is a lot of work to do before I can start sending and receiving messages. I don't need a real time chat yet. Its just a messaging system that looks like a chat. I need a chathistory too, not a chat only!

Thoughts about SSE and Ajax:

As described in this Answer: Performance of AJAX vs Websocket REST over HTTP 2.0?

I'm Afraid that I have a lot of work with this: "Unlike Websockets, both SSE and Http+AJAX are stateless, so you need to go through authentication for every new message cycle, decoding the Http/2 headers, encoding the Http/2 headers and using all the resources related to a fresh request..."

Final question

Should I stop here and do it with nodejs and some nice npm packages instead of finish the PHP solution?

user_name
  • 129
  • 7
  • PHP is a templating language and not a server. WebSockets are a server feature. So all you can do is set a timer on the client and make HTTP requests to see if there are any notifications. Anything else requires you to find a different server solution. (i.e. look at NodeJS as it supports WebSockets natively). – Reactgular Dec 06 '17 at 19:13
  • You have covered two strategies available to you when using PHP in your answer. HTTP is stateless protocol, so by definition you will have to do everything to authorize new request everytime, however SESSION helps to make your life easier and the overhead is not terrible if you do minimize your backend code. I have done in the past what you are looking for using jQuery and it is really fast, efficient and does not take much time to develop - it is as simple as it gets in AJAX.... – Tadasbub Dec 06 '17 at 19:20
  • For your application a basic timed ajax implementation sounds like it will do the job fine. Traffic will be minimal in both directions, if all you need to do it set a message icon on or off then you could do it conditionally on http response code and your php doesnt even need to return any body content – miknik Dec 06 '17 at 19:23
  • You can use something called BOSH. https://en.wikipedia.org/wiki/BOSH_(protocol) – Marco Dec 06 '17 at 20:03
  • Thank you for your answers! BOSH seems to be a bit unknown but efficient - why does not everybody use it? What are the disadvantages? Looks like it needs some installation. What do you think abaout Server Sent Events? This looks quite simple to me... – user_name Dec 06 '17 at 20:42
  • BUT: SSE connections keep a mobile device’s radio powered up all the time.": https://www.fastly.com/blog/server-sent-events-fastly/ Slowly comes the feeling that I do not get very far with PHP! :-/ – user_name Dec 06 '17 at 20:53

0 Answers0