I was wondering, if it is possible to connect (as an admin) with a user and to write messages? I already modified the message-table in the database, however, the message does only appear after reloading the whole website - which is not the optimal outcome. I appreciate every hint. Thank you.
You have to use a pub/sub service to get notification on the client side when a message is sent in server side
A good start is pusher.com service, in your server side code, you will run this example code whenever you send a message to your user:
pusher.trigger('my-channel', 'my-event', {
'message': 'hello world'
})
and in your client side, you add a code that listen to the above channel/topic and act accordingly
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
alert('Received my-event with message: ' + data.message);
});
Related
I have configured and have the code written out in my Django app to integrate Twilio SMS sending and receiving. It is successfully sending messages.
However, the problem I'm coming across is that the public facing URL I have to communicate with the webhook and TwiML is only accessible for the admin so you have to have a certain authentication to access the messaging dashboard (where the messages are coming in). I'm not sure how to configure this in Twilio
I was wondering how I can configure this webhook in Twilio to ensure a user can respond via SMS from their phone and the message will display in the messaging dashboard within the app.
I'm not super familiar with Twilio or this situation so any help would be greatly appreciated. Thanks in advance!
Twilio's serverless offering, Functions, could help you out here. It's not available in Python but uses Node.js instead. However, it's pretty straightforward, allows you to call your own URLs with any authentication mechanisms, and helps you store secrets securely.
Here's a small sample snippet from the docs:
const axios = require('axios');
exports.handler = async (context, event, callback) => {
const twiml = new Twilio.twiml.MessagingResponse();
const response = await axios
.get('https://dog.ceo/api/breeds/image/random');
twiml
.message(`Hello, ${event.From}! Enjoy this doge!`)
.media(response.data.message);
return callback(null, twiml);
};
I'm trying to get a var to my consumers.py to send data to the client in real time as a function does API calls and returns that to the browser.
I know channels needs Redis to function, but why? Why can we not just pass a list as it's built to the consumers class or any variable for that matter? From another answer: to store the necessary information required for different instances of consumers to communicate with one another. But what if I only will use one websocket connection, and only one user is allowed to be logged in at a time? This will be locally hosted only and the function is outside of consumers.py that returns the data so subscribing to groups may be where I need these.
Am I missing something or is redis / memurai a must here? I just can't help but to feel there's an easier way.
I ended up using server side events (SSE), specifically django-eventstream, and so far it's worked great as I didn't need the client to interact with the server, for a chat application this would not work.
Eventstream creates an endpoint at /events/ the client can connect to and receive a streaming http response.
Sending data from externalFunc.py:
send_event('test', 'message', {'text': 'Hello World'})
Event listener in HTML page:
var es = new ReconnectingEventSource('/events/');
es.addEventListener('message', function (e) {
console.log(e.data);
var source = new EventSource("/events/")
var para = document.createElement("P");
const obj = JSON.parse(event.data)
para.innerText = obj.text;
document.body.appendChild(para)
}, false);
es.addEventListener('stream-reset', function (e) {
}, false);
I would like to write a bot that will listen to the specific chat and save messages to the local storage.
After starting the bot app, everything works properly, but after some time I can see such information in the logs:
pyrogram.client {
'_': 'types.UpdatesTooLong'
}
Immediately after this log, my userbot stops hearing any message from any user.
How can I fix this and handle such case updatesTooLong ?
I want to send a message to a websocket client when it connects to the server on AWS lambda and API gateway. Currently, I use wscat as a client. Since the response 'connected' is not shown on the wscat console when I connect to the server, I added post_to_connection to send a message 'hello world' to the client. However, it raises GoneException.
An error occurred (GoneException) when calling the PostToConnection
operation
How can I solve this problem and send some message to wscat when connecting to the server?
My python code is below. I use Python 3.8.5.
import os
import boto3
import botocore
dynamodb = boto3.resource('dynamodb')
connections = dynamodb.Table(os.environ['TABLE_NAME'])
def lambda_handler(event, context):
domain_name = event.get('requestContext',{}).get('domainName')
stage = event.get('requestContext',{}).get('stage')
connection_id = event.get('requestContext',{}).get('connectionId')
result = connections.put_item(Item={ 'id': connection_id })
apigw_management = boto3.client('apigatewaymanagementapi',
endpoint_url=F"https://{domain_name}/{stage}")
ret = "hello world";
try:
_ = apigw_management.post_to_connection(ConnectionId=connection_id,
Data=ret)
except botocore.exceptions.ClientError as e:
print(e);
return { 'statusCode': 500,
'body': 'something went wrong' }
return { 'statusCode': 200,
"body": 'connected'};
Self-answer: you cannot post_to_connection to the connection itself in onconnect.
I have found that the GoneException can occur when the client that initiated the websocket has disconnected from the socket and its connectionId can no longer be found. Is there something causing the originating client to disconnect from the socket before it can receive your return message?
My use case is different but I am basically using a DB to check the state of a connection before replying to it, and not using the request context to do that. This error's appearance was reduced by writing connectionIds to DynamoDB on connect, and deleting them from the table upon disconnect events. Messaging now writes to connectionIds in the table instead of the id in the request context. Most messages go through but some errors are still emitted when the client leaves the socket but does not emit a proper disconnect event which leaves orphans in the table. The next step is to enforce item deletes when irregular disconnections occur. Involving a DB may be overkill for your situation, just sharing what helped me make progress on the GoneException error.
We need to post to connection after connecting (i.e. when the routeKey is not $connect)
routeKey = event.get('requestContext', {}).get('routeKey')
print(routeKey) # for debugging
if routeKey != '$connect': # if we have defined multiple route keys we can choose the right one here
apigw_management.post_to_connection(ConnectionId=connection_id, Data=ret)
#nemy's answer is totally true but it doesn't explain the reason. So, I just want to explain...
So, first of all What is GoneException or GoneError 410 ?
A 410 Gone error occurs when a user tries to access an asset which no longer exists on the requested server. In order for a request to return a 410 Gone status, the resource must also have no forwarding address and be considered to be gone permanently.
you can find more details about GoneException in this article.
In here, GoneException has occured; it means that the POST connection we are trying to make, doesn't exist - which fits perfectly in the scenario. Because we still haven't established the connection between Client and Server. The way APIGatewayWebsocketAPIs work is that you request an Endpoint(Route) and that Endpoint will invoke that Lambda Function (In our case it is ConnectionLambdaFunction for $connect Route).
Now, if The Lambda function resolves with statusCode: 200 then and only then the API Gateway will allow the connection to be established. So, basically untill we return statusCode: 200 from our Lambda Function we are not connected and untill then we are totally unknown to server and thats why the Post call that has been made before the return statement itself will throw an error.
I want to implement an instant messaging server using Flask + Flask-soketIO.
with client side on mobile phone (front in Ionic 2)
I have already tried different chat room examples with socketIO but I wonder how to manage multiple users chatting two by two.
I'm not yet familiar with instant messaging architectures. I have several questions on the subject :
first of all, is Flask a good framework to implement instant messaging for mobile phone application ?
I did start with Flask because it seems powerful and not heavy as django can be.
In instant messaging app with sokcetIO, how can I connect users two by two?
I tried this code, but it works for multiple users in the same tchat room :
On the client side :
<script type="text/javascript">
$(document).ready(function() {
var socket = io.connect("http://127.0.0.1:5000");
socket.on('connect', function() {
console.log('connected')
});
socket.on('message',function(msg){
$("#messages").append('<li>' + msg + '</li>');
});
$("#sendButton").on('click', function() {
console.log($('#myMessage').val());
socket.send({ 'author': 'Kidz55',
'message': $('#myMessage').val()});
$('#myMessage').val('');
});
});
</script>
On the server side :
#socketio.on('message')
def handle_json(json):
print('received json: ' + str(json))
# broadcasting to everyone who 's connected
send(json,,broadcast=True)
Is it scalable, and does it support heavy traffic ?
In instant messaging app with sokcetIO, how can I connect users two by two?
If it is always going to be two users chatting, then they can send direct messages to each other. When a client connects, it gets assigned a session id, or sid. If you keep track of these ids and map them to your users, you can then send a message to specific users. For example, if you store the sid value for a user in your user database, you can then send a direct message to that user as follows:
emit('private_message', {'msg': 'hello!'}, room=user.sid)
Is it scalable, and does it support heavy traffic ?
There are many factors that influence how much traffic your server can handle. The Flask-SocketIO server is scalable, in the sense that if a single process cannot handle the traffic, you can add more processes, basically giving you a lot of room to grow.