[PYTHON] Push notification to Intel Edison

What is push notification?

When a computer is connected to the Internet to obtain information, most of them access a server on the Internet from the client side and receive the necessary information. The opposite route to this is the access called "push notification", in which the server side notifies the client side of the information. In familiar places, you may receive a phone call or a message via Skype / LINE. When the client receives information from the server, there is another method called "polling", in which the client asks the server for new information at regular intervals. Actually, this push notification is really Taihen when I try to implement it myself. This will be explained in the next chapter.

Technical difficulty

When connecting to the Internet (www), many computers in the world have a router (broadband router) in between to separate the local network (LAN) from www. The router governs the network by assigning a unique IP address (private address) to each computer within the LAN only within the LAN. Then, when the computer in the LAN communicates with the computer in www, the private address of the computer in the LAN is converted into a global IP address that can be used in www so that it can communicate (NAT / IP masquerade). It also plays a role. いんたぁねっとの図 With this configuration, there is no problem when accessing www from a computer in the LAN, but conversely, when trying to access a computer in the LAN from a computer on the www side, the computer in the LAN is in www. Cannot access because there is no global IP address identified by.

Workaround

NAT traversal is not yet an established technology, so there is no definitive measure. This time, I will introduce two types for the time being. ** We received a comment from voluntas about STUN / TURN explained below. ** See comments.

Through the server

In this method, terminals that want to communicate with each other connect to the same server, and that server relays the communication between them. In particular, there is TURN as a standardized method. This method requires a considerable amount of traffic and server load, but it is easy to implement and </ s> can overcome almost all NAT.

STUN STUN is the port number of what kind of global IP address possessed by the terminals that are trying to communicate with each other </ s>. This is a method in which the server actually on www tells you whether you are connected to www by number, and the terminals communicate with each other via P2P using that information. With this method, the server only notifies the terminal of the profile once at the beginning, so high-speed communication between terminals is possible regardless of the specifications of the server. However, STUN has restrictions on the types of NAT that can be exceeded.

Experiment

It's such a difficult NAT traversal, but let's easily realize it using an external service. PubNub is a well-known service that supports such push notifications. Another option is to use the Google Cloud Platform Channel API. However, this time, I will use the Streaming API of Twitter, which everyone loves and I love too. In order to access Twitter from an application, you need to register your own application, issue a consumer key, and obtain an access token for the account you want to access. Refer to this.

push.py


#!/usr/bin/python
# -*- coding: utf-8 -*-

from twitter import *
import mraa, time

gpio = mraa.Gpio(20)
gpio.dir(mraa.DIR_OUT)

def blink():
  for i in range(20):
    gpio.write(0 if i%2==0 else 1)
    time.sleep(0.3)

auth = OAuth(
    consumer_key = "iruafhiiUEHWIiuHFWIUli",
    consumer_secret = "Eiuhf4fFW0fwwfehi2fWEiWooifwQEIUUfh2efFui",
    token = "428639810-dwufhiEhi04FWIi3Lluh3fwiuhIWhi2fi8fwuFWf",
    token_secret = "fo9f3fwufwiGiuHF57Fui1ohifut7kfwuFKwufhwuF"
    )

allow_users = ['k_yone']

twitter_userstream = TwitterStream(auth=auth, domain='userstream.twitter.com    ')
for msg in twitter_userstream.user():
  if 'user' in msg:
    user = msg['user']
    if 'screen_name' in user:
      print("user: %s " % user['screen_name'])
      if user['screen_name'] not in allow_users:
        continue

      print msg['text']
      if msg['text'] == u"L Chika":
        blink()
        print "success"

The program looks like this. Twitter's Streaming API keeps the communication path established between the client and the server, so if there is an action on the server, the result will be sent to the client immediately. NAT traversal using this function. The data that can be sent is text data of 140 characters or less, but depending on the device, this may be sufficient. It is also attractive that you can use the authentication mechanism of Twitter as it is.

result

The video is here.

Summary

  • NAT traversal is Taihen
  • There are some external services to support
  • Twitter's Streaming API has been used

Recommended Posts