I have a chat feature in my app using JSQMessagesViewController (https://github.com/jessesquires/JSQMessagesViewController).
It makes an API call to my server every 5 seconds to get all the chat messages between two users to populate the chat view with those messages. It was suggested I turn that into a socket connection because making an API call every 5 seconds will be hard on the battery and a waste of API calls.
How do I turn my chat feature into a socket connection with my server?
Are there any libraries in Objective-C that will help me accomplish this? And also, I'm pretty sure that I'll have to set this up on the back end too, so are there any resources as to how to set this up on a PHP Laravel backend?
You should be able to accomplish this using a couple mature libraries on both the iOS frontend and the PHP backend.
iOS: SocketRocket
https://github.com/square/SocketRocket
PHP:: Ratchet
http://socketo.me
They both support RFC 6455 so should integrate pretty easily.
Since you are using PHP, I'm going to take a stab and guess you are using Apache? If so, here is some more information on setting up websockets to work with Apache which can have issues with connection management: Setting up a websocket on Apache?
Related
I am trying to implement a very simple self-hosted (docker) pubsub client update system using websockets or SSE for my PHP application.
I would like that a specific javascript action on my website triggers a php server-side update (via ajax) which should then lead to an update to all subscribed devices for the particular channel/topic. The subscribed devices should use javascript listening to one channel each.
Do you know any simple PHP ws libraries which are capable of that? Can SSE be triggered in that way and also utilize channels? How about in-built PHP socket function? Ideally I would like to stick to PHP solution in order not to deal with translations and in-between REST APIs.
After researching this for days and going through solutions like RabbitMQ, Redis, Kafka, RatchetPHP, ZeroMQ, socket.io, RPC, SSE and etc I am completely lost. I need some guidance on which way to go.
Among all possible solutions here is how I would do it:
Install a web socket server, preferably implemented in PHP, on you Linux system.
Your web clients register with the server and listen for messages like broadcast from the server.
Your ajax script registers also with the server and sends a broadcast to the server if all work is done.
The server passes the broadcast to all registered clients.
You could use my implementation of such a web socket server that comes with all the needed client
libraries for JavaScript as well as for PHP that implement broadcast and feedback.
See https://github.com/napengam/phpWebSocketServer
I love programming using PHP. Recently I've wondered wether to spend more time learning node.js or not. Then I stumbled across PHP websocket. I saw an example making a live chat in websocket, where messages was sent in live time.
However, I can't seem to find any documentation or, better yet, explanation of what this feature actually does! Can anyone explain what PHP websocket does (technically), or send link to the correct documentation?
Wouldn't PHP websocket make it possible to push any type of content in real time to users, just like node.js can? Or have I misunderstood?
WebSockets are a bi-directional persistent connection from a web browser to a server. Read WebSocket on WikiPedia and Mozilla Web Docs
node.js and WebSocket are 2 different things. node.js is programming language built on the Google V8 JavaScript engine, whereas WebSocket is communication protocol.
Yes, WebSocket can help you push real-time updates to your client.
Here is One of the site that can help you work in WebSocket using PHP - http://socketo.me - Documentation URL : http://socketo.me/docs/
I am working on a little personal project on Ionic2 framework, and one part of my application requires a chat to be implemented. So I went and read about Socket.IO and it's implementations and it is really awesome. But the drawback for my project is that the frontend will not be able to host NodeJS, so I had to quit the idea of using Socket.io.
is there any alternative/Solution to make the PHPserver maintain a bidirectional connection for the chat? My guessing was to use GET/POST between the Ionic App and the server but that will be heavy on both client and server since it has to check on every short amount of time (less than a second to give the real-time like behavior).
Thank you.
I'm looking into the development of a site where socket use is a must for real time updates. I understand with wordpress it is difficult to incorporate node.js and socket.io and was wondering if this is the case with drupal. Specifically could I incorporate node.js and socket.io with php? What specific steps would I need to take? I appreciate the help and of course if you give me a good answer, I'll rate you up.
There are currently Node.js modules that connect to Drupal, Joomla, and WordPress. I wrote the one for Joomla: https://github.com/jlleblanc/nodejs-joomla You can find all of these through NPM. First, you would download one of these modules, then you would also download the Socket.io module. Then you would write server side code connecting the CMS to Socket.io. Finally, you would write client-side code to talk to the socket running in Node.
You can have it separated and since you want some client-server communication with node.js, you can have it - outside Drupal (this is the easiest and cleanest way).
CMS you use (be it Drupal or Wordpress) does not limit your JavaScript from using socket.io and node.js for the calls you want to be made outside the CMS server-side code. You can eg. read the same database Drupal/Wordpress does, but using Node.js and returning the results directly to the client-side JavaScript script.
You can integrate Socket.IO with whatever technology you like, but you need to think of them as 2 separate parts that need to communicate.
I see 2 solutions for Socket.IO to communicate with an external service (Drupal, Joomla, Wordpress, whatever):
1) Making a rest API for your Socket.IO application (for example using Express for the API layer and then passing messages to Socket.IO with an EventEmitter).
2) The better solution in my opinion is to use a message queue like Redis, RabbitMQ or ZeroMQ. The Drupal website would send a message down the channel to Socket.IO and then Socket.IO would send that message to the client(s).
If you are creating a system that needs authentication also, I suggest you don't let your users send message directly with Socket.IO, but make an Ajax call to your main server instead (Drupal for ex.). That way you can check the user's identity more easily. (This scenario would be a fit for a chat based system that requires authentication)
Although this video tutorial is for EventMachine with Faye, the same logic could apply to your app: http://railscasts.com/episodes/260-messaging-with-faye
I was looking at Kanaka's reply to a websockets question here.
If I want to write a native app in PHP to use a websocket based webservice, where do I start? Are there any client libraries available in PHP to write native linux clients?
Basically, my server can work on a RESTful architecture for 90% of the time. However, there is one reason for the server to alert the client, so REST won't work here. So I am wondering if websockets is an answer here for me as compared to periodic polling by client.
Why not write the 90% using normal REST and do the remaining part where notifications are sent to the client using web sockets?
Here is a github project that looks like it might be what you want https://github.com/nicokaiser/php-websocket