I would like to create a live notification center by using Web Socket in Python.
I have a feature that send / emit a message to specific user.
I have read about socket id and room. I think there are a couple way to do direct user interaction using both feature from Socket IO.
First:
It is said in the docs that every client / socket connected to the socket server, they were giving an unique socket#id and joined a room identified by id.
I have an idea that I do not need create a room again for a user, and just by using this default room.
By using these, I want to create a mapping of socket#id with userid (logged user id), and store it into redis. Like, if I want to send a message to a user id, I just need to search for it's socket#id.
The question for this first way is, is the socket#id will be the same
if a client disconnect and reconnect soon the connection can be
established ?
Second:
I have read many sources about using room for direct message. For connected client or socket, I just need to create a new room with id of the user id that sent from client (after user login).
The advantages of this way is, if the user is using same user id for several device, I can group them in a room. So, if I want to notify to all device, I just need to send the message for room of id user.
The question of this way, is the user will be moved from default room
from first way or they will be on 2 room (1 default, 1 specified room
by user id) ?
Thank You
This is the way i do it
a users socketid will be different on every page request, and if a user have 3 tabs open he will have a different socketid on every tab, so if you want a user to get the notification in every tab u need to send it to 3 socketids
a solution for this is to make an object of connected users, you can add the userid/username as a key in your object and make an array of all socketids for this user, make sure to remove the socketid when the user disconnect too, and when u want to send a notification u just loop over the array and send to all of the users socketids
The question for this first way is, is the socket#id will be the same if a client disconnect and reconnect soon the connection can be established ?
No. Each connection gets a randomly assigned socket id.
The question of this way, is the user will be moved from default room from first way or they will be on 2 room (1 default, 1 specified room by user id) ?
Users can be in multiple rooms at a time. If you create a room with your application generated user ids, and then put all the socket connections from the user into that room, each of these clients will be in two rooms, the one that matches the socket id and the one that matches your user id. You can then emit a message to each socket individually or to all sockets that belong to the user.
Related
I wanna code a telegram bot, so when I gonna receive messages from a user I should know about last message he/she sent to me and in which step does he/she located. So I should store sessions of the user (I understood this when I searched) but I don't know what exactly should I do?
I know I need a table in a db that stores UserId, ChatId but I don't know these:
How to make a root for steps and store them in db (I mean how do I understand where the user is located now)
What are other columns that I need to store as a session?
How many messages should I store in the database? And do I need one row for each message?
If you just have to store session in your database you don't need to store messages. Maybe you could want to store also messages but it's not necessarily related.
Let's assume you have a "preferences" menu in your bot where the user can write his input. You ask for the name, age, gender etc.
How do your know when the user writes the input of it's about the name or the gender etc?
You save sessions in your db. When the bot receives the message you check in what session the user is in to run the right function.
An easy solution could be a sql database.
The primary key column is the telegram user ID ( you additionally can add a chat id column if it's intended to work both in private and group chats) and a "session" column TEXT where you log user steps. The session column can be NULL by default. If the bot expects the gender (because the user issued /gender command) you can update the column "session" with the word "gender" so when the message arrives you know how to handle it checking the gender column of that user id and as soon as you runned the right function, you update to NULL again the column "session".
you can create a db with these columns.
UserID, ChatID, State, Name, Age, Gender ...
on each incoming update you will check if user exists on you db then check the user's State and respond appropriately and update the state at the end.
I would like to find out whether one can use a script to query a user some data through kannel.
Usecase
Consider a scenario where I need to carry out a registration process
via sms. I need to query for the name, birthdate, gender and so forth.
Suggestions highly appreciated.
Progress
I have tried doing so using the sms-service;
# SMS SERVICE
group = sms-service
keyword = limo
get-url = "http://localhost:3000/client?sender=%p&text=%k"
accept-x-kannel-headers = true
max-messages = 3
concatenation = true
In this case I am relying on get-url to extract the sender's msisdn. Upon receipt of the keyword "limo", I would like to start prompting the user for their name, birthdate, gender etc...in a step wise manner.
When a user sends "limo", I will respond using a question, for example, "Reply with your name?". The user may text back "Willy". I would like to retrieve and store this in the data base and prompt for the birthdate which i will in turn also store in the database.
The challenge is to extract these responses effectively from smsbox.log as well as handle the session.
Yes, this is possible. Setting up Kannel for this is pretty much straight forward (https://jasonrogena.github.io/2014/01/18/kannel-and-the-huawei-e160.html). You will, however, have to manage your sessions on your own as Kannel is stateless.
Update
You don't need to extract anything from the log files (processing log files is highly discouraged). Everything you need in this scenario can be passed into your script (specified by the get-url variable)
I think the GET request variables you have specified in our get-url are a bit fucked up. Use "http://localhost:3000/client?phone=%p&text=%a" instead.
Handling sessions is pretty easy:
In your database, have a table that will store sessions. Make the sender's phone number the primary key for this table. The table should probably have another column for storing the last registration step (in your case, you can declare it as an ENUM('name','birthdate','gender')). I'll refer to this column as the last_step column.
When your script is called by Kannel, check if the sender's number is in the session table.
If the sender is not in the session table, add a new row with the primary_key = the phone number and last_step = 'name'. You should probably then send the sender a text message asking them to provide their name.
If the sender is in the session table, check what the value of last_step is for that particular number is. If last_step = 'name', your code should assume that the sender has just sent their name. Store the received SMS as the name in your DB. Update last_step = 'birthdate' then send an SMS to the sender asking them to provide their birth date.
Follow this logic until the user finishes the registration process.
I am trying to get historical data for all of our Yammer posts. We are very active, and like to look back sometimes. The data is loaded into a data warehouse.
For current messages, I get a list of users and then match messages against the user list. I can then use the email address as a unique identifier for the user (our users likes to change their names on the network to goofy names, or to someone else's name) to match it up with the rest of the user information we have for them.
The problem is that I when I start going back in time with the messages, the only information I get about a user is the ID and the username for users that are suspended or deleted. I can't seem to establish a way to match the username against a person. Some of the usernames contain periods (firstname.lastname), and other don't.
Is there any way to get the email address of the suspended/deleted user?
Is there away to know how many person connects to a session?
I wish to implement a mechanism where only two persons are allowed to connect, and if there is more than two, an alert will be shown.
When you get a sessionConnected event, you get an array of connections telling you the number of people in the session. http://www.tokbox.com/opentok/docs/js/reference/SessionConnectEvent.html
After you are connected, everytime someone joins the session, you get ConnectionEvents for people leaving and joining the session.
These events can give you an accurate count of the # of connections in the session at any given time
I am building a website that will notify users of certain changes to their account via a notification bar at the top of the screen (similar to the way SO notifies of new badges).
In my DB I have a table that list the events and also a flag to denote whether they have been notified of the change yet or not. The table is of the form :
UserID AccountEvent EventDescription HasBeenNotified
ABC123 1 Your price deal ends in 2 weeks FALSE
What I would like to achieve is:
Customer logs in and is shown notification (assuming one needs to
happen)
Customer clicks to acknowledge message
DB HasBeenNotified field set to TRUE (to acknowledge notification has occurred)
I have been able to achieve all this but there is a snag!
I am putting the events that need to be notified into the HttpRuntime.Cache with a 10 min expiry so I don't need to keep polling the DB.
The issue this creates for me is that a customer could potentially see multiple notifications of the same message until the cache expires, for example:
Customer logs in and app logic looks in cache and says customer ABC123 needs to be
notified of X
Customer then acknowledges message (which updates DB to set notification flag to TRUE)
Cache does not yet reflect updated DB so if customer navigates to new page the same notification message is fired (until cache expires)
As far as I know I cannot update the actual data within the Cache (can only Remove data from cache based on cache key).
Can anybody provide hints and tips of how to get around the issue of multiple notifications?
Why not burst the cache? Remove that key from the cache?
Or you can update the cache by setting it again to new HasBeenNotified value or even set it to null which you should deal with and accept as empty.
HttpRuntime.Cache["YourKey"] = "";//new object/value;