I already have a web server set up with NGINX showing a live webcam feed from the mjpg streamer as part of a laser tag game where the idea is for the user to be able to view a live camera stream by going to a local website. However, at this stage I would like to add motion detection capabilities by using the motion package, as shown here to process the mjpg live stream.

The reason I don't want to simply use motion is because it has more of a delay than mjpg-streamer. When the user is viewing the live camera feed from a page on the local web server, I would like for the user to be notified if there is any motion detection (possibly through a JavaScript alert). Motion allows for any command to be run through the command line if motion is detected.

Is there a shell command (maybe even to start a python script, etc.) which could communicate real-time (AJAX setup on the web server?) to the live HTML page showing the live camera feed running on the client device?

I would really appreciate it if you could answer my question and help me with this issue! Thanks for taking the time to read the question!


Ok, so I've been doing some research, and I believe (but I am not completely sure) that a publish-subscribe API could work for my purposes. I found PubNub to be a popular for real-time communication. I have also found some people using it with the Raspberry Pi like in this example (I am comfortable with C programming so I would prefer to use it to work with PubNub).

Looking around their website, I found it is possible to also use PubNub with JavaScript as well, and I was curious to see if the two languages might be able to work together with PubNub. Theoretically, I would call a C program which would publish a message to the javascript on my NGINX web server, subscribed to the same channel. Finally, the JavaScript might respond with an alert. I think this would all happen without reloading the page.

I have not completely solved the problem yet, so I am curious to see if others would have any thoughts on how to implement this.

  • If there this question is off-topic, please tell me in case I need to ask elsewhere. Thank you!
    – iRove
    Commented May 31, 2016 at 1:41
  • Here is where I found JavaScript to be supported with PubNub: JavaScript API Reference for Realtime Apps | PubNub
    – iRove
    Commented Jun 1, 2016 at 0:34
  • It's not exactly off-topic but it is bordering on "too broad" and "unclear" -- not something I would answer personally anyway. I would suggest that AJAX has been superseded by websockets (which would be fairly simple to implement server-side in C and have client side support in js everywhere now); I think it should even be possible to do a bit of a redirect including the port so you could use a dedicated websocket based server for the feed (I'm not positive about that nor an nginx user, in which context I think it might be more easily accomplished by a peer connection on the server).
    – goldilocks
    Commented Jun 1, 2016 at 1:12
  • I think I might have solved the issue but I have yet to try it out. I am going along with the PubNub update I provided earlier. I found here that folks over at PubNub used different technologies to detect the motion, but then they used python to notify JavaScript. If I can do the same in C, I could notify the JavaScript application listening on the channel. I might be getting somewhere here; now I have a little proof my update idea might work. By the way, thanks @goldilocks for the feedback.
    – iRove
    Commented Jun 1, 2016 at 1:58
  • With that said, do you guys think there is anything I can do to improve the clarity of the question?
    – iRove
    Commented Jun 1, 2016 at 15:40

1 Answer 1


As I suggested earlier, PubNub turned out to be the perfect solution to this problem.

Basically, I used PubNub's Posix C API to connect to my client's web browser. So, the motion daemon would execute the C program, which in turn would publish to the PubNub service. The JavaScript would be subscribed to the same channel, and would pick up the message and play an alert on the web browser.

Links to the C and JavaScript publish/subscribe tutorials:

  • Posix C Publish/Subscribe: here
  • JavaScript Publish/Subscribe: here

With these resources, connecting was actually fairly simple. Just sign up for a PubNub account, create an 'app', and create a key to use for connecting the two devices.

However, the only hiccup I ran into was trying to use PubNub's new c-core library on GitHub; I could not install it on the raspberry pi. I had to user their older C library which was used in the tutorial I provided in my question with my update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.