Communicate with a specific client [JS/PY] - python

I use angular client side (TS) and Django server side (PY).
Unfortunately despite what I find on the internet, I can't send a message to a SPECIFIC client from the server. Socket.io doesn't exist in PY, I've seen the websockets but that only talks about sending a message to ALL clients in a group. But if I want to send a message only to a client B I don't see how to do it.
Please help me, and guide me on the solution to use
PS: I'm starting with the PY backend, be indulgent.
Thank you.

Related

Connecting to external web socket api from Django

I am trying to connect to slack RTM (Real Time Messaging) api and update the database with the messages coming from the slack channel. How should I do that.
Should I run a web-socket client in the background? If so, how to integrate the web-socket client with the Django application.
Is Celery something that I should look into? I was looking into this for the past few hours. Went through a lot of questions. I am not able to figure out the right approach to do that.
You would need a TCP socket.
You will figure out more here: https://realpython.com/python-sockets/

How to let server send the message first in GRPC using python

Hi i am new to GRPC and i want to send one message from server to client first. I understood how to implement client sending a message and getting response from server. But i wanna try how server could initiate a message to connected clients. How could i do that?
Short answer: you can't
gRPC is a request-response framework based on HTTP2. Just as you cannot make a website that initiates a connection to a browser, you cannot make a gRPC service initiating a connection to the client. How would the service even know who to talk to?
A solution could be to open a gRPC server on the client. This way both the client and the server can accept connections from one another.

Stream information through a WebSocket received through ZeroMQ in Python

currently, I have a little script that receives a lot of information through various channels using ZeroMQ PUB-SUB sockets.
What i want to do is: receive the info with zmq, process it, and then, send it away through a WebSocket, in order to be able to use it in an AngularJS application, displaying real time information.
All the websocket libraries i have found for python work in an asynchronous way, where i can send messages as a responde to a message the browser send in advance. I don't want to send messages as a response, i want to stream all the information I am receiving, for any instance of my AngularJS app that might be running.
Thanks in advance.
EDIT: I forgot something. I found this How do I stream data through a flask application?, and got it working with flask, but it seems to only work with the flask app from which the websocket is created, i can't connect to it from angular.
One good example is on Miguel Grinberg blog here. He creates a flask backend and uses Angular on front-end.
After the browser open the channel, the back end can send messages any time, without any constraints related to timing.

Slack RTM API uses POST or GET method?

This might seem a lame question, but would be great if someone can help. I wrote a small python script which returns some output based on a command sent to it via slack's custom bot. Python script uses RTM API. Whenever someone in slack mentions the bot and passes a command (ex: #slackcustombot foobar), it returns a custom message(ex: Hi foobar) from the script. Now the issue is at the security side. My script resides at a server which is not open to internet. And as Slack uses dynamic IP, there's no possibility of white-listing the slack ip's.
So basically, all I wanted to know is, when someone in slack channel mentions the bots, and when the bot at slack server sends the command to my python script residing at my server, does it send as GET method or as POST method? cuz if it's a GET method, I might not have to worry about the security issue. but if it sends as POST method, what alternative I could use in this scenario? Any help is appreciated. Please correct me if I am wrong somewhere, still learning. :)
Regards,
Junaid.
Neither. It uses the WebSocket protocoll. If you rather want to use a HTTP-based protocol I would recommend looking into the Slack's Event API. It uses HTTPS POST to transfer messages to your bot.
The Events API is not providing all event types that are available with the RTM API, but its much easier to handle and should be sufficient for a chat bot. Check here for a documentation of which event types are available to the both RTM API and Events API.
You will however need to find a way to expose the url of your bot to the Internet, so that Slack can use it. If you need to access internal company applications through your Slack bot, the best approach in my opinion is to have the Slack bot on a webserver in the DMZ of your company.
A more small scale approach is to use a forwarding provider that is connecting to your local webserver through a VPN tunnel and exposing your internal webserver through a special public URL. That would work if you want Slack to talk to your webserver on a local network behind a router / firewall. One example is ngrok, but they are other providers too.

Implementing Backend IP Messaging Twilio

So I'm kind of stuck. I'm trying to implement Twilio ip messaging for our app and I'm not sure I'm finding what I need in the docs.
From what I see as examples - the only things that the backend is responsible for is the credential generation - something I have done.
Should the backend also not somehow facilitate the message sending and conversation generation?
Any useful information/documentation links/tutorial links would be much appreciated.
Twilio developer evangelist here.
The message sending and conversation generation can all be done through the client libraries, either the JavaScript or iOS (or, coming soon, Android) SDKs. That way, other than the credential generation as you pointed out, your servers don't need to be responsible for the rest of the chat application.
You can, if you choose, send messages from your own server too. Check out the documentation on the IP Messaging REST API.
If you want your server to interact with messages sent by the client libraries you can also subscribe to webhooks for IP Messaging too.
Let me know if this helps at all.

Categories

Resources